After a long time in Ubuntu, I finally succeeded in operating MySQL in C. Here I will write down the method and save it for later use. First install MySQL code: sudoapt-getinstallmysql-servermysql-client and then install the Development Kit code: sudoapt-getinstalllibmysqlclient15-dev can use the following code to test the code:/* SimpleCprogramtha
After a long time in Ubuntu, I finally succeeded in operating MySQL in C. Here I will write down the method and save it for later use. Install MySQL first
Code:
Sudo apt-get install mysql-server mysql-client
Reinstall the Development Kit
Code:
Sudo apt-get install libmysqlclient15-dev
Use the following code to test
Code:
/* Simple C program that connects to MySQL Database server */
# Include
# Include
Main (){
MYSQL * conn;
MYSQL_RES * res;
MYSQL_ROW row;
Char * server = "localhost ";
Char * user = "root ";
Char * password = "";/* change the password here */
Char * database = "mysql ";
Conn = mysql_init (NULL );
/* Connect to database */
If (! Mysql_real_connect (conn, server,
User, password, database, 0, NULL, 0 )){
Fprintf (stderr, "% s \ n", mysql_error (conn ));
Exit (1 );
}
/* Send SQL query */
If (mysql_query (conn, "show tables ")){
Fprintf (stderr, "% s \ n", mysql_error (conn ));
Exit (1 );
}
Res = mysql_use_result (conn );
/* Output table name */
Printf ("MySQL Tables in mysql database: \ n ");
While (row = mysql_fetch_row (res ))! = NULL)
Printf ("% s \ n", row [0]);
/* Close connection */
Mysql_free_result (res );
Mysql_close (conn );
}
Compile:
Method 1: Run gcc test. c-o test-I/user/include/mysql and the system prompts that mysql. h cannot be found.
I tried it and prompted that mysql. h could not be found.
Analysis:
/User/include/mysql: All. H files, which should be compiled
/User/lib/mysql: it is a static library and a dynamic library. www.linuxidc.com should correspond to the above. h file for the connection phase.
Method 2: gcc test. c-o test-L/user/lib/mysql-lmysqlclient succeeded!
Method 3: gcc test. c-o test $ (mysql_config -- cflags) $ (mysql_config -- libs)
Or: gcc test. c-o test $ (mysql_config -- cflags -- libs) is successful!