This afternoon idle nothing to do, the team leader let me familiar with the C + + connection database, and operation, the whole process is smooth, in order to remember and next use, now the whole process is recorded:
1. Install MySQL
Slightly
2. Create a C + + console program and create a new CPP source file such as: Sqlconn.cpp
3. Add the Mysql\mysql\mysql Server 5.7\include in the MySQL installation directory to the additional include directory in the project properties->c/c++-> General
4. Add Library Directory
5. Add dependency "Libmysql.lib"
6. Change the running platform to X64 (this step is important, otherwise the compilation will be error)
7. Add the header file to the source file:
#include <winsock.h>//order of attention, to be placed in front of mysql.h
#include <mysql.h>//console project to include <winsock.h> before mysql.h
8. Write your own database operating procedures
Example:
void Test1 ()
{
MYSQL *pconn;
Pconn = Mysql_init (NULL);
The meanings of the 2nd, 3, 4, and 5 parameters are: Server address, user name, password, database name, and 6th MySQL port number (0 is the default value of 3306).
if (!mysql_real_connect (Pconn, "localhost", "root", "root", "test", 0,null,0))
{
printf ("Unable to connect to database:%s", Mysql_error (Pconn));
Return
}
mysql_query (Pconn, "Set names GBK");//prevent garbled characters. The settings are consistent with the database encoding and are not garbled.
Set NAMES x equals set character_set_client = x; SET character_set_results = x; SET character_set_connection = x;
Write set character set GBK; the query will not be garbled, but the parameterized insert will error. and set names GBK is not garbled.
Mysql_real_query is more than mysql_query parameter: the length of the string query, so it is suitable for query with binary data, and mysql_query string query cannot contain binary because it ends with a
mysql_query () cannot pass a binary blob field, because the \ \ In binary information is misjudged as the end of the statement. Mysql_real_query () is available.
if (mysql_query (Pconn, "SELECT * from persons"))
{
printf ("Query failed:%s", Mysql_error (Pconn));
Return
}
Mysql_store_result is an offline data set that takes the results of a query to the client at once, and consumes memory when the results are relatively large.
Mysql_use_result is the result of the query placed on the server, the client through the pointer read by line, saving client memory. But a mysql* connection can have only one mysql_use_result query that is not closed at the same time
Mysql_res *result = Mysql_store_result (pconn);
Mysql_row ROW;
while (row = mysql_fetch_row (result))
{
printf ("%s%s\n", row[1],row[2]);
}
Mysql_free_result (result);
Mysql_close (Pconn);
}
9. Place the MYSQ directory ..... \mysql\mysql Server 5.7\lib under the Libmysql.dll into the generated EXE directory, compile and execute.
Reference:
1.http://www.cnblogs.com/rupeng/archive/2012/10/06/2712841.html
2.http://bbs.csdn.net/topics/390523114
C + + operation MySQL Database