Install MySql in Ubuntu to obtain the C interface created by MySQL. h.
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-server mysql-client and then install the development package...
Install MySql in Ubuntu to obtain the C interface created by MySQL. h.
[Date: 2010-02-05] Source: Ubuntu community Author: intijk
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 <mysql. h>
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 <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 );
}