Install MySQL first
Code: Sudo apt-Get install mysql-server mysql-Client
Reinstall the Development KitCode:Sudo apt-Get install libmysqlclient15-dev
After installation, add the header file to the C code.Code:# Include <mysql. h>
Compilation Method:Code:GCC $ (mysql_config -- cflags) xxx. C-o XXX $ (mysql_config -- libs)
Use the following code to testCode:/* Simple C program that connects to MySQL database server */
# Include <mysql. h>
# Include <stdio. h>
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.