Connect to MySQL using C language in Linux

Source: Internet
Author: User
Insert operation:
# Include <stdio. h> # include <MySQL. h> int main (INT argc, char * argv []) {MySQL conn; int res; mysql_init (& conn); // initialize the connection if (mysql_real_connect (& Conn, "localhost", "root", "123", "test", 0, null, client_found_rows) {printf ("database connection successful/N "); res = mysql_query (& Conn, "insert into test values ('user', '000000')"); // If the insert statement is successful, 0 is returned and 1if (RES) is returned) {printf ("statement execution failed/N"); mysql_close (& conn); // remember to close the connection} else {printf ("statement execution successful/N "); mysql_close (& conn) ;}} return 0 ;}
Query operation:

 

Void query_ SQL (char * SQL) {MySQL my_connection;/* This is a database connection */INT res;/* The return sign 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, column; /* query the number of returned rows and columns */INT I, j;/* only two variables controlling the loop * // * initialize MySQL connection my_connection */mysql_init (& my_connection ); /* MySQL is used here. A function in H uses the macros we previously defined to establish a MySQL connection and return a value. The return value is not null to prove that the connection is successful */If (mysql_real_connect (& my_co Nnection, host, username, password, database, 0, null, client_found_rows) {/* connection successful */printf ("database query query_ SQL connection successful! N ");/* sets the query encoding to utf8, which supports Chinese characters */mysql_query (& my_connection," set names utf8 "); /* the following statement uses the mysql_query function to execute the SQL statement we just passed in. This will return an int value. If it is 0, verify that the sentence execution is successful */RES = mysql_query (& my_connection, SQL); If (RES) {/* now indicates that the execution failed */printf ("error: mysql_query! N ");/* do not forget to close the connection */mysql_close (& my_connection );} else {/* now indicates that the execution is successful * // * Send the query result 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) + 1; printf ("% lu row N", row);/* field name of the output result */for (I = 0; field = mysql_fetch_field (res_ptr); I ++) printf ("% ST", field-> name); printf ("N");/* result output by line */for (I = 1; I <row; I ++) {result_row = mysql_fetch_row (res_ptr); For (j = 0; j <column; j ++) printf ("% ST", result_row [J]); printf ("N") ;}}/* do not forget to close the connection */mysql_close (& my_connection );}}}

Compile: gcc-O test_mysql test_mysql.c 'mysql _ config -- cflags -- libs'

Insert, delete, update, and return an integer to determine whether the operation is successful or not. The returned results are more complex.

 

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.