Original address: http://blog.csdn.net/nupt123456789/article/details/8043091
Installation of 1.MySQL databases
You can download the MySQL database installation package (http://download.csdn.net/detail/nuptboyzhb/4619847) from MySQL's official website or from the address below. This article takes Mysql-5.0.27-win32 as an example. Unzip the installation after the download is complete. Note: During the installation process, select "Complete" to install, and do not select the default "typical". No, there is no connection library associated with C + +. Then always click Next. The installation process allows you to register, you will be done on a step-by-step.
2. Establishment of the database
You can create a database and a table directly from the MySQL command-line window. However, I strongly recommend that you use visual tools (you can search for them). Here, I have previously installed WAMP5 directly with the web version of the phpMyAdmin tool. If you have installed WAMP5, enter it directly in the browser: http://localhost/phpmyadmin/. Then we create a new database named TestDB and create a new table in it: name_table. Then add data: ZHB 22studentsnjupt
Configuration of the 3.vc++6.0
This article takes the classical vc++6.0 as an example. (Of course, the VS2005 2008 2010, etc., is also configured in this way). Open the Directory (tab), vc++6.0, tools, options, and add MySQL's include path in its include files. For example, the path to my MySQL include folder is: C:\Program files\mysql\mysql Server 5.0\include. Toggle the dropdown box, select Library files, and add the MySQL lib path. As my: C:\Program files\mysql\mysql Server 5.0\lib\opt
4. Program connection database and query
#include <windows.h>#include<stdio.h>#include<stdlib.h>#include<string.h>#include<mysql.h>#include<iostream>#pragmaComment (lib, "Libmysql.lib")//libraries needed to connect to MySQLusing namespacestd;intMain () {Const CharUser[] ="Root";//username Const CharPswd[] ="*********";//Password Const CharHost[] ="localhost";//or "127.0.0.1" Const CharTable[] ="TestDB";//DatabaseUnsignedintPort =3306;//Server PortMYSQL Mycont; Mysql_res*result; Mysql_row Sql_row; Mysql_field*FD; Charcolumn[ +][ +]; intRes; Mysql_init (&Mycont); if(Mysql_real_connect (&mycont,host,user,pswd,table,port,null,0) ) {cout<<"Connect succeed!"<<Endl; mysql_query (&mycont,"SET NAMES GBK");//set the encoding format, otherwise the Chinese cannot be displayed under CMDRes=mysql_query (&mycont,"SELECT * from Name_table");//Enquiry if(!Res) {Result=mysql_store_result (&mycont);//Save the queried data to result if(Result) {inti,j; cout<<"Number of result:"<< (unsignedLong) mysql_num_rows (Result) <<Endl; for(i=0; Fd=mysql_fetch_field (result); i++)//Get column name{strcpy (column[i],fd-name); } J=mysql_num_fields (Result); for(i=0; i<j;i++) {printf ("%s\t", Column[i]); } printf ("\ n"); while(Sql_row=mysql_fetch_row (Result))//get the specific data { for(i=0; i<j;i++) {printf ("%s\n", Sql_row[i]); } printf ("\ n"); } } } Else{cout<<"Query SQL failed!"<<Endl; } } Else{cout<<"Connect failed!"<<Endl; } if(result!=null) mysql_free_result (result);//Releasing result ResourcesMysql_close (&mycont);//Disconnect Connection return 0;}
"Turn" VC + + connection to MySQL database