MySQL C Language API:
The MySQL client "C" language API development libraries and "C" header files are available as MySQL development packages. On my Red Hat Enterprise 6 workstation it is mysql-devel-5.1.47-4.el6.x86_64
The MySQL client API has been ported to many platforms with many different compilers. To help developers determine the compiling and linking flags to use on their platform use the mysql_config utility program:
- C compile flags: mysql_config --cflags
Result: -I/usr/include/mysql -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -D_GNU_SOURCE
-D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -fno-strict-aliasing -fwrapv -fPIC
-DUNIV_LINUX
- Linker flags: mysql_config --libs
Result: -rdynamic -L/usr/lib64/mysql -lmysqlclient -lz -lcrypt -lnsl -lm -lssl -lcrypto
Example MySQL C API program:
Two examples:
Insert and select from an existing database:
This example will use the following example database schema and contents:
01 | CREATE DATABASE bedrock; |
03 | CREATE TABLE employee (IDpk integer NOT NULL auto_increment, |
04 | Employee_Name char (20), |
08 | INSERT INTO employee (Employee_Name,Dept,JobTitle) VALUES ( 'Fred Flinstone' , 'Worker' , 'Rock Digger' ); |
09 | INSERT INTO employee (Employee_Name,Dept,JobTitle) VALUES ( 'Wilma Flinstone' , 'Finance' , 'Analyst' ); |
10 | INSERT into employee (Employee_Name,Dept,JobTitle) VALUES ( 'Barney Rubble' , 'Sales' , 'Neighbor' ); |
Example C/C++ program:
File: testMySqlAPI.cpp
010 | FFError( ) { Label = ( char *) "Generic Error" ; } |
011 | FFError( char *message ) { Label = message; } |
013 | inline const char * GetMessage ( void ) { return Label.c_str(); } |
024 | MYSQL *MySQLConnection = NULL; |
026 | string hostName = "localhost" ; |
027 | string userId = "user1" ; |
028 | string password = "supersecret" ; |
029 | string DB = "bedrock" ; |
031 | MySQLConnection = mysql_init( NULL ); |
035 | MySQLConRet = mysql_real_connect( MySQLConnection, |
044 | if ( MySQLConRet == NULL ) |
045 | throw FFError( ( char *) mysql_error(MySQLConnection) ); |
047 | printf ( "MySQL Connection Info: %s \n" , mysql_get_host_info(MySQLConnection)); |
048 | printf ( "MySQL Client Info: %s \n" , mysql_get_client_info()); |
049 | printf ( "MySQL Server Info: %s \n" , mysql_get_server_info(MySQLConnection)); |
054 | printf ( "%s\n" ,e.Label.c_str()); |
059 | MYSQL_RES *mysqlResult = NULL; |
067 | mysql_free_result(mysqlResult); |
076 | string sqlInsStatement = "INSERT INTO employee (Employee_Name,Dept,JobTitle) VALUES ('Betty Rubble','IT','Neighbor')" ; |
077 | mysqlStatus = mysql_query( MySQLConnection, sqlInsStatement.c_str() ); |
080 | throw FFError( ( char *)mysql_error(MySQLConnection) ); |
085 | printf ( "%s\n" ,e.Label.c_str()); |
086 | mysql_close(MySQLConnection); |
092 | mysql_free_result(mysqlResult); |
100 | MYSQL_FIELD *mysqlFields; |
101 | my_ulonglong numRows; |
102 | unsigned int numFields; |
106 | string sqlSelStatement = "SELECT * FROM employee" ; |
107 | mysqlStatus = mysql_query( MySQLConnection, sqlSelStatement.c_str() ); |
110 | throw FFError( ( char *)mysql_error(MySQLConnection) ); |
112 | mysqlResult = mysql_store_result(MySQLConnection); |
117 | numRows = mysql_num_rows(mysqlResult); |
120 | numFields = mysql_field_count(MySQLConnection); |
123 | numFields = mysql_num_fields(mysqlResult); |
125 | printf ( "Number of rows=%u Number of fields=%u \n" ,numRows,numFields); |
129 | printf ( "Result set is empty" ); |
134 | mysqlFields = mysql_fetch_fields(mysqlResult); |
136 | for ( int jj=0; jj < numFields; jj++) |
138 | printf ( "%s\t" ,mysqlFields[jj].name); |
144 | while (mysqlRow = mysql_fetch_row(mysqlResult)) |
146 | for ( int ii=0; ii < numFields; ii++) |
148 | printf ( "%s\t" , mysqlRow[ii] ? mysqlRow[ii] : "NULL" ); |
155 | mysql_free_result(mysqlResult); |
161 | printf ( "%s\n" ,e.Label.c_str()); |
162 | mysql_close(MySQLConnection); |
169 | mysql_close(MySQLConnection); |
Note:
- The query string is NOT terminated with a ";"
- The function mysql_query() accepts a string query
- The function mysql_real_query() accepts a binary query
Compile: g++ -o testMySqlAPI testMySqlAPI.cpp `mysql_config --cflags` `mysql_config --libs`
Run: ./testMySqlAPI
MySQL Connection Info: Localhost via UNIX socket
MySQL Client Info: 5.1.47
MySQL Server Info: 5.1.47
Number of rows=4 Number of fields=4
IDpk Employee_Name Dept JobTitle
1 Fred Flinstone Worker Rock Digger
2 Wilma Flinstone Finance Analyst
3 Barney Rubble Sales Neighbor
4 Betty Rubble IT Neighbor
Generate a new database and table and insert a record:
File: genDatabase.cpp
10 | FFError( ) { Label = ( char *) "Generic Error" ; } |
11 | FFError( char *message ) { Label = message; } |
13 | inline const char * GetMessage ( void ) { return Label.c_str(); } |
24 | MYSQL *MySQLConnection = NULL; |
26 | string hostName = "localhost" ; |
27 | string userId = "user1" ; |
28 | string password = "supersecret" ; |
30 | MySQLConnection = mysql_init( NULL ); |
34 | MySQLConRet = mysql_real_connect( MySQLConnection, |
43 | if ( MySQLConRet == NULL ) |
44 | throw FFError( ( char *) mysql_error(MySQLConnection) ); |
46 | printf ( "MySQL Connection Info: %s \n" , mysql_get_host_info(MySQLConnection)); |
47 | printf ( "MySQL Client Info: %s \n" , mysql_get_client_info()); |
48 | printf ( "MySQL Server Info: %s \n" , mysql_get_server_info(MySQLConnection)); |
53 | printf ( "%s\n" ,e.Label.c_str()); |
61 | if (mysql_query(MySQLConnection, "CREATE DATABASE adams" )) |
63 | printf ( "Error %u: %s\n" , mysql_errno(MySQLConnection), mysql_error(MySQLConnection)); |
70 | if (mysql_query(MySQLConnection, "USE adams" ) ) |
72 | printf ( "Error %u: %s\n" , mysql_errno(MySQLConnection), mysql_error(MySQLConnection)); |
79 | if (mysql_query(MySQLConnection, "CREATE TABLE family (Name char(20),Room char(8),Phone char(24))" ) ) |
81 | printf ( "Error %u: %s\n" , mysql_errno(MySQLConnection), mysql_error(MySQLConnection)); |
85 | if (mysql_query(MySQLConnection, "INSERT INTO family VALUES ('Gomez Adams', 'master', '1-555-1212')" ) ) |
87 | printf ( "Error %u: %s\n" , mysql_errno(MySQLConnection), mysql_error(MySQLConnection)); |
94 | mysql_close(MySQLConnection); |
Compile: g++ -o genDatabase genDatabase.cpp `mysql_config --cflags` `mysql_config --libs`
Run: ./genDatabase
Check MySQL for the database, table and data:
mysql> use adams;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
mysql> show tables;
+-----------------+
| Tables_in_adams |
+-----------------+
| family |
+-----------------+
1 row in set (0.00 sec)
mysql> select * from family;
+-------------+--------+------------+
| Name | Room | Phone |
+-------------+--------+------------+
| Gomez Adams | master | 1-555-1212 |
+-------------+--------+------------+
1 row in set (0.00 sec)
Links:

Books:
 |
"MySQL, Second edition"
by Paul DuBois
ISBN # 0735712123, New Riders Publishing
|
|
 |
"Managing and Using MySQL"
by George Reese, Randy Jay Yarger, Tim King
ISBN # 0596002114, O'Reilly
|
|
 |
"MySQL Cookbook"
by Paul DuBois
ISBN # 0596001452, O'Reilly
|
|
 |
"High Performance MySQL"
by Jeremy D. Zawodny, Derek J. Balling
ISBN # 0596003064, O'Reilly
|
|
 |
"The Definitive Guide to MySQL, Second Edition"
by Michael Kofler
ISBN # 1590591445, APress
|
|
 |
"MySQL Certification Study Guide"
by Paul Dubois, Stefan Hinz, Carsten Pedersen
ISBN # 0672326329, Sams
|
|