Connect C ++ to MySQL under codeblocks

Source: Internet
Author: User

C ++ connects to the MySQL database

C ++ is not as intelligent as PHP and does not have an integrated Wamp environment. Therefore, C ++ is less convenient to connect to the database. After checking some information online, the connection was successful and the process was recorded.

1.Install MySQL on your computer

Some people on the Internet say that the custmo mode must be selected, but I did not select it during installation and the connection was successful. It seems unnecessary.

2.Download the MySQL header file

This must be available. After downloading, put the. h file in it under the include folder of codeblocks. This step aims to reference header files such as MySQL. h In a Write Program.

3. in codeblocks, choose Project-> build options-> setting and link, add, and find libmysql. lib, mysqlclient. lib, mysqld. the three lib files add all the paths of these three files to make codeblocks able to find and compile. If it is installed by default, the three files are located in c: \ Program Files \ mysql \ MySQL Server 5.1 \ Lib, and each file may have different locations.

4. Try to connect to the database. If the connection fails, "libmysql. dll is not found, so this application cannot be started. "Re-installing the application may fix this problem ." Solution:

4.1: Copy libmysql. dll under the c: \ mysql \ Lib \ opt directory to the C: \ windows \ system32 directory.

4.2: Copy libmysql.dllbeibei to the file where .exe is located.

Finally, a successful connection code is attached:

# Include <iostream> # include <stdio. h> # include <winsock2.h> # include <MySQL. h> using namespace STD; /* database connection macro */# define host "localhost" # define username "root" # define password "" # define Database "test" Void query_ SQL (char * SQL) {MySQL my_connection;/* This is a database connection */INT res;/* The return flag after the SQL statement is executed */mysql_res * res_ptr; /* pointer to the query result */mysql_field * field;/* field structure pointer */mysql_row result_row;/* query information returned by row */INT row, Colum N;/* query the number of returned rows and columns */INT I, j;/* initialize MySQL connection my_connection */mysql_init (& my_connection ); /* Create a MySQL connection */If (null! = Mysql_real_connect (& my_connection, host, username, password, database, 0, null, 0)/* connection successful */{printf ("query_ SQL database query connection successful! \ N ");/* set the query encoding to GBK to support Chinese characters */mysql_query (& my_connection," set names GBK "); Res = mysql_query (& my_connection, SQL ); cout <"res =" <res <Endl; If (RES)/* execution failed */{printf ("error: mysql_query! \ N "); cout <mysql_error (& my_connection) <Endl;/* close the connection */mysql_close (& my_connection );} else/* now indicates that the execution is successful */{/* and the query result is returned to res_ptr */res_ptr = mysql_store_result (& my_connection);/* if the result is not empty, print */If (res_ptr) {/* The number of rows and */column = mysql_num_fields (res_ptr); ROW = mysql_num_rows (res_ptr ); printf ("% d row \ n", row);/* field name of the output result */for (I = 0; field = mysql_fetch_field (res_ptr ); I ++) printf ("% 10 s", field-> name); printf ("\ n");/* result output by line */for (I = 1; I <row + 1; I ++) {result_row = mysql_fetch_row (res_ptr); For (j = 0; j <column; j ++) printf ("% 10 s ", result_row [J]); printf ("\ n") ;}/ * do not forget to close the connection */mysql_close (& my_connection );}} else {printf ("database connection failed") ;}} int main () {char * query; query = "select * from student"; query_ SQL (query); Return 0 ;}
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: 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.