Linux c programming to access the database, linux to access data
The source code is as follows:
# Include <stdio. h> # include <stdlib. h> # include <mysql/mysql. h> int main () {/* defines the mysql variable */MYSQL * coon; // The handle MYSQL_RES * res used for database connection; // returns the row query result MYSQL_ROW row; // a piece of data in the record set: char server [] = "192.168.1.105"; char user [] = "gino"; char password [] = "123456 "; char database [] = "mysql"; coon = mysql_init (NULL); // initialize and obtain the mysql handle/* connect to the database */if (! Mysql_real_connect (coon, server, user, password, database, 0, NULL, 0) {fprintf (stderr, "% s \ n", mysql_errno (coon )); exit (1);}/* execute mysql statement */if (mysql_query (coon, "show tables") {fprintf (stderr, "% s \ n ", mysql_errno (coon); exit (1);}/* initialize the row-by-row result set */res = mysql_use_result (coon); printf ("MySQL Tables in mysql datables: \ n "); // retrieve the next row from the result set while (row = mysql_fetch_row (res ))! = NULL) {printf ("% s \ n", row [0]);} // release the result set using multiple memory mysql_free_result (res ); // close the server connection mysql_close (coon); printf ("finish \ n"); return 0 ;}
-Lmysqlclient is required for compilation after mysql API is used.
Install libmysqlclient:
sudo apt-get install libmysqlclient-dev
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.