C ++ operates mysql Databases and mysql Databases

Source: Internet
Author: User

C ++ operates mysql Databases and mysql Databases

This afternoon I had nothing to do. The team lead familiarized me with connecting C ++ to the database and operating on it. The whole process was still smooth. In order to remember and use it for the next time, now the entire process is recorded:

1. Install mysql

Omitted

2. Create a C ++ console program and create a CPP source file, such as sqlconn. cpp.

3. In the project properties-> c/C ++-> General-> Add mysql \ MySQL Server 5.7 \ include in the MySQL installation directory to the include directory

4. Add a library directory

5. Add the dependency "libmysql. lib"

 

6. Change the running platform to X64 (this step is very important, otherwise an error will be reported during compilation)

7. Add the header file to the source file:

# Include <winsock. h> // note the sequence before mysql. h.
# Include <mysql. h> // In the console project, include <winsock. h> before mysql. h.

8. Write your own database operating procedures

Example:

Void test1 ()
{
MYSQL * pConn;
PConn = mysql_init (NULL );
// The parameters 2nd, 3, 4, and 5 respectively mean the server address, user name, password, and database name. The 6th parameters are the mysql port number (0 indicates the default value 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"); // prevents garbled characters. If the encoding is consistent with that of the database, no garbled characters will be generated.
// Set names x is equivalent to SET character_set_client = x; SET character_set_results = x; SET character_set_connection = x;
// Write set character set gbk. The query will not contain garbled characters, but an error will be reported during parameterized insertion. Set names gbk won't contain garbled characters


// Mysql_real_query has more parameters than mysql_query: the length of the string query, so it is suitable for queries with binary data. The string query of mysql_query cannot contain binary data because it ends with \ 0.
// Mysql_query () cannot pass binary BLOB fields, because \ 0 in the binary information will be misjudged as the end of the statement. Mysql_real_query.
If (mysql_query (pConn, "select * from persons "))
{
Printf ("query failed: % s", mysql_error (pConn ));
Return;
}

// Mysql_store_result retrieves the query result to the offline dataset of the client at one time. memory consumption is high when the result is large.
// Mysql_use_result indicates that the query result is stored on the server, and the client reads data row by row through the pointer, saving the client memory. However, a MYSQL * connection can only have one unclosed mysql_use_result query.
MYSQL_RES * result = mysql_store_result (pConn );
MYSQL_ROW row;
While (row = mysql_fetch_row (result ))
{
Printf ("% s \ n", row [1], row [2]);
}

Mysql_free_result (result );
Mysql_close (pConn );
}

9. Set the mysq directory ..... Put libmysql. dll under \ MySQL Server 5.7 \ lib in the generated exe directory and compile and execute it.

 

Refer:

1. http://www.cnblogs.com/rupeng/archive/2012/10/06/2712841.html

2. http://bbs.csdn.net/topics/390523114

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.