[MySQL and MFC] VC ++ database connection

Source: Internet
Author: User
First install MySQL, vs2008
Add the following code to the. cpp file of the VC ++ project:
# Include <winsock2.h> // define socket # include "MySQL. H "// # pragma comment (Lib," libmysql. lib "); // Add libmysql to the attached dependency. lib functions are the same
The following configuration in project properties: 1. Add the directory containing the header file mysql. H, include is the mysql-5.0.27-win32 \ include folder. 2, additional library directory, MySQL lib file is mysql-5.0.27-win32 \ Lib \ opt file 3, additional dependencies, name libmysql. lib4: Convert libmysql. DLL copy to the debug folder, libmysql. DLL in the Lib folder
Define global SQL
MYSQL mysql;
Mysql_init (& MySQL); // initialize bool isconnected = mysql_real_connect (& MySQL, "localhost", "root", "131015", "firstsqldb", 3306, 0, 0 ); // Connect if (isconnected) {MessageBox (_ T ("Success connected! "); Return;} else {int I = mysql_errno (& MySQL); // connection error const char * s = mysql_error (& MySQL ); messageBox (_ T ("failed to connect! Please check for your DB service. ");} mysql_close (& MySQL); // close sqlmysql_res * result; mysql_row SQL _row; mysql_field * FD; int res = mysql_query (& MySQL, "select * From mytable"); // execute the SQL command if (! Res) {result = mysql_store_result (& MySQL); // Save the queried data to result if (result) {int I, j; for (I = 0; FD = mysql_fetch_field (result); I ++) // obtain the column name {STR [I]. format ("S", FD-> name); forshow = forshow + STR [I] + "\ t";} J = mysql_num_fields (result ); while (SQL _row = mysql_fetch_row (result) // obtain the specific data {for (I = 0; I <j; I ++) ss. format ("S", SQL _row [I]);} If (result! = NULL) mysql_free_result (result) ;}} else {MessageBox (_ T ("query SQL failed! "));}

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.