First, the development environment
Win8.1 64-bit, VS2013, MySQL5.5.3764-bit
the MySQL installation directory is: C:\Program files\mysql\mysql Server 5.5
Second, the configuration of the engineering environment
First create a console empty project, open VS20 13, file --> New project--General--Select" Empty Project "
Because we want to use the MySQL database API programming, so we need to the project additional header file search directory and attachment library file search directory, point to the MySQL installation directory corresponding location, below is my machine MySQL library and header file directory:
Point the additional header file directory and additional library directory of the VS2013 project to the top two directories.
1> Configuration Header file directory Open the Project Configuration Properties window--C + +----Add the
include directory to the MySQL include directory Add to the attached include directory as shown in:
2> Configuration library file directory
Open the Project configuration window----the linker--and the additional library directory, add the MySQL lib directory to the additional library directory, as shown in:
Open the Project configuration window --linker--input-to-middle dependencies , hit Libmysql.lib to add a static library to the project compilation dependency, as shown in:
Copy the libmysql.dll Dynamic Library to the project's root directory or the debug directory:
Third, test development environment
#include <stdio.h> #include <stdlib.h> #include <windows.h> #include <mysql.h>void testquery ( MySQL *mysql);//test query data void Main () {MySQL *mysql = null;/* initialize mysql connection handle */mysql = Mysql_init ((MySQL *) 0); if (!mysql) {return ;} /* Connect to the database, the connection successfully returns conn, otherwise returns a null parameter 1:mysql_init the MySQL handle returned by the initialization database parameter 2: Database server address Parameter 3: Database user name parameter 4: Database password parameter 5: database name parameter 6: Database port, 0 means the default 3306 parameter 7: If Unix_socket is not NULL, the string specifies the socket or the named pipe that should be used. Note The host parameter determines the type of connection parameter 8: usually 0*/mysql = Mysql_real_connect (mysql, "localhost", "root", "root", "test", 0, NULL, 0); if (MySQL) { printf ("Connection succellfull the database!\n");} else{printf ("Connection error:%d,%s\n", Mysql_errno (MySQL), mysql_error (MySQL));} Query data testquery (MySQL);//close connection mysql_close (MySQL); System ("Pause");} Test query void Testquery (MYSQL *mysql) {Mysql_row ROW; Mysql_res *res = NULL; Mysql_field *fields = Null;int I, Field_count;char *sql = "SELECT * from T_user"; int flag = mysql_real_query (MySQL, SQL, ( unsigned long) strlen (SQL)), if (flag) {printf ("Query error:%d,%s\n", Mysql_errno (MySQL), mysql_Error (MySQL); return;} Read the query results into memory res = Mysql_store_result (mysql);//Get all fields in the result set field = Mysql_fetch_fields (res);//number of fields Field_count = MySQL _field_count (MySQL); for (i = 0; i < Field_count; i++) {printf ("%s\t", Fields[i].name);} printf ("\ n");//traverse the result set for each row of data while (row = mysql_fetch_row (res)) {for (i = 0; i < Field_count; i++) {printf ("%s\t", Row[i]); }printf ("\ n");}}
MySQL test data and table structure:
DROP TABLE IF EXISTS ' T_user '; CREATE TABLE ' t_user ' ( ' id ' int (one) not null auto_increment, ' name ' varchar (+) DEFAULT NULL, ' age ' int (one) D Efault null, ' address ' varchar (+) default NULL, PRIMARY KEY (' id ')) engine=innodb auto_increment=3 Default Charset=utf8; LOCK TABLES ' t_user ' write;insert into ' T_user ' VALUES (1, ' Zhangsan ', +, ' Hunan '), (2, ' Lisi ', +, ' Beijin '); UNLOCK TABLES;
Test Results:
Summary configuration steps:
1> installing MySQL
2> Create vs Project, configure the project header file (mysql.h directory) and library file (Libmysql.lib directory) additional directory, point to MySQL corresponding directory
3> Copy the Libmysql.dll dynamic library to the project root directory or the debug directory
4> Write test program, verify C connect MySQL database
----------------------------------------------------------------------Elegant split-line--------------------------------------------- ---------------------------------------
Common errors:
1, main.obj:error LNK2019: unresolved external symbol mysql_init .....
The reason is that the libmysql.lib configuration is not added to the project, the configuration library file directory
2, unable to start this program, because the computer lost libmyslq.dll ....
Copy the Libmysql.dll dynamic library to the project's root directory or the debug directory.
3, did not introduce windows.h header file, because in Windows connection MySQL is the socket way to communicate with the database
4, Main.obj:error LNK2019: unresolved external symbol [email protected], the symbol is referenced in the function _main .....
The number of database bits is inconsistent with the number of compile bits, resulting in MySQL library functions not being found at link time . For example: My MySQL is 64 bit, the library provided is of course 64 bit, if you on the VS on the 32-bit platform to compile will cause the link error.
Modify the build platform, engineering--Properties--Configuration Manager--Select the corresponding project in the solution's project list and modify it to either 32-bit or 64-bit, or create a new one if not.
Project source Download: Windows platform C connection MySQL database
Reference Documentation:
A/C + + connection MySQL Database