C + + Connect MySQL database

Source: Internet
Author: User
Tags mysql version sprintf

1. Create a project using VS

2. Right-click the project selection properties, go to C + + + General + + Add included directory, adding D:\mysql-5.7.19-win64\include (according to your own installation of MySQL version and address fill)

3. In the property page, go to the linker = General + Add Library directory, adding D:\mysql-5.7.19-winx64\lib (fill in your case)

4. Add Libmysql.dll (added from the computer's Explorer) in the project's. \x64\debug\. dll file in the D:\mysql-5.7.19-winx64\lib directory

5. Code Testing

#include "stdio.h" #include "mysql.h" int main () {mysql * con;//= mysql_init ((mysql*) 0);    Mysql_res *res;    Mysql_row ROW;    Char tmp[400];  Database Configuartion char dbuser[30] = "root"; MySQL account name char dbpasswd[30] = "123456";    MySQL account password char dbip[30] = "localhost"; Char dbname[50] = "student";//Database name char tablename[50] = "BL";    Data table name Char *query = NULL;    int x;    int y;    int Rt;//return value unsigned int t;    int count = 0;    con = Mysql_init ((mysql*) 0); if (con! = NULL && mysql_real_connect (Con, Dbip, Dbuser, dbpasswd, dbname, 3306, NULL, 0)) {if (!mysql_sel            Ect_db (Con, dbname)) {printf ("Select successfully the database!\n");            Con->reconnect = 1;            query = "Set names \ ' Gbk\ '";            RT = Mysql_real_query (Con, query, strlen (query)); if (RT) {printf ("Error making query:%s!!!            \ n ", mysql_error (Con)); } else {printf ("queRy%s succeed!\n ", query);    }}}} else {MessageBoxA (NULL, "Unable to connect the Database,check your configuration!", "", null); }//sprintf (TMP, "insert into%s values (%s,%d,%d)", TableName, "null", X, y);    Note how to insert record sprintf (TMP, "INSERT INTO BL values" (NULL, ' X ', ' x ', ' x ', ' X ') to the database with the self-increment field;    RT = Mysql_real_query (Con, TMP, strlen (TMP)); if (RT) {printf ("Error making query:%s!!!    \ n ", mysql_error (Con)); } else {printf ("%s executed!!!    \ n ", TMP);    } sprintf (tmp, "SELECT * from%s", TableName);    RT = Mysql_real_query (Con, TMP, strlen (TMP)); if (RT) {printf ("Error making query:%s!!!    \ n ", mysql_error (Con)); } else {printf ("%s executed!!!    \ n ", TMP); } res = Mysql_store_result (con);//Save the result in the Res struct while (row = mysql_fetch_row (res)) {for (t = 0; T<mysql_n Um_fields (RES);        t++) {printf ("%s", Row[t]);        } printf ("... ... \ n");; count++;    } printf ("Number of rows%d\n", count);    printf ("mysql_free_result...\n");    Mysql_free_result (RES);    Mysql_close (con);    System ("pause"); return 0;}

C + + connection to MySQL database

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.