Complete example of accessing mysql using C language
1. manually install mysql2 with mysql sdk. create a console project. add C:/Program Files/MySQL Server 5.5/include to the project attribute and add C: /Program Files/MySQL Server 5.5/lib add "Library Directory" 3. stdafx. add # include in h
// Pay attention to the sequence, which should be placed before mysql. h # include
// The console project must be included before mysql. h.
// Note that lib is added to the "Library Directory" instead of the "reference directory" # pragma comment (lib, "libmysql. lib ") 4. set libmysql. dll is placed in the generated exe directory 5. main examples: // execute the basic query 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 SET to the same as that of the database, no garbled characters will occur. // set names x is equivalent to SET character_set_client = x; SET character_connection = x; // write set character set gbk; the query will not be garbled, but an error will be reported during parameterized insertion. Set names gbk does not contain garbled characters. // mysql_real_query has more parameters than mysql_query: the length of the string query. Therefore, 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 (), the binary BLOB field cannot be passed, because/0 in the binary information is 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 );} // Obtain the number of updated rows void test2 () {MYSQL * pConn; pConn = mysql_init (NULL); if (! Mysql_real_connect (pConn, "127.0.0.1", "root", "root", "test", 0, NULL, 0) {printf ("unable to connect to database: % s ", mysql_error (pConn); return;} if (mysql_query (pConn, "update persons set Age = Age + 1") {printf ("execution failed: % s ", mysql_error (pConn); return;} printf ("update successful, total Updated % d", mysql_affected_rows (pConn); mysql_close (pConn );} // Obtain the auto-increment idvoid test3 () {MYSQL * pConn; pConn = mysql_init (NULL); if (! Mysql_real_connect (pConn, "127.0.0.1", "root", "root", "test", 0, NULL, 0) {printf ("unable to connect to database: % s ", mysql_error (pConn); return;} mysql_query (pConn, "set names gbk"); if (mysql_query (pConn, "insert into persons (Name, Age) values ('chuanzhi pods', 100) ") {printf (" failed to execute insert % s ", mysql_error (pConn); return;} printf (" successful to execute insert, new id = % d ", mysql_insert_id (pConn); mysql_close (pConn);} // void test4 () {MYSQL * pConn; pConn = Mysql_init (NULL); if (! Mysql_real_connect (pConn, "127.0.0.1", "root", "root", "test", 0, NULL, 0) {printf ("database connection failed: % s ", mysql_error (pConn); return;} mysql_query (pConn, "set names gbk"); MYSQL_STMT * stmt; MYSQL_BIND bind [2]; memset (bind, 0, sizeof (bind); // set the default values of is_null, length, and other fields to NULL and other default values; otherwise, stmt = mysql_stmt_init (pConn) will be reported during execution ); char * insertSQL = "insert into persons (Name, Age) values (?,?) "; If (mysql_stmt_prepare (stmt, insertSQL, strlen (insertSQL) {fprintf (stderr," mysql_stmt_prepare (), INSERT failed, % s/n ", mysql_error (pConn); return;} bind [0]. buffer_type = MYSQL_TYPE_STRING; bind [0]. buffer = ""; bind [0]. buffer_length = strlen (""); // If buffer_length is set, you can skip the length int age = 3; bind [1]. buffer_type = MYSQL_TYPE_LONG; bind [1]. buffer = & age; bind [1]. buffer_length = sizeof (age); if (mysql_stmt_bind_param (stmt, bind) {fprintf (stderr, "failed () failed % s/n", mysql_stmt_error (stmt )); return;} if (mysql_stmt_execute (stmt) {fprintf (stderr, "mysql_stmt_execute (), failed % s/n", mysql_stmt_error (stmt); return ;} mysql_stmt_close (stmt); mysql_close (pConn); printf ("SQL completion of parameterized execution ");}