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
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: sudo apt-get install mysql
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
After installation, add the header file to the C code.
Code:
# Include
Compilation Method:
Code:
Gcc $ (mysql_config -- cflags) xxx. c-o xxx $ (mysql_config -- libs)
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 );
}
The content of the existing database and table is output.