This post has already been read 2545 times!

MySQL provide C libraries that enable the creation of MySQL database applications.

The MySQL API is much larger than the in order to account for MySQL's extended feature set. In many cases, MySQL is actually only providing an API interface into database administration functions that are present in both database engines.

By just reading the function names, you might have gathered that any database application you write might minimally look something like this:

1. Connect
2. Select DB
3. Query
4. Fetch row
5. Fetch field
6. Close

A simple program that Selects All Data in a test database and Displays the data :

#include 
#include 
#include 
#include 

int main()
	{
	MYSQL_RES *result;
	    MYSQL_ROW row;
	    MYSQL *connection, mysql;
	    int state;

	     /* connect to the mySQL database at athens.imaginary.com */
	    mysql_init(&mysql);
	    connection = mysql_real_connect(&mysql,
	                                    "localhost",
	                                    "root",
	                                    "",
	                                    "database",
	                                    3306,
	                                    "/var/lib/mysql/mysql2.sock",
	                                    0);
	    /* check for a connection error */
	    if( connection == NULL ) {
	        /* print the error message */
	        printf(mysql_error(&mysql));
	        return 1;
	    }
	    state = mysql_query(connection,
	                        "SELECT adresse_id, adresse FROM adresse");
	    if( state != 0 ) {
	        printf(mysql_error(connection));
	        return 1;
	    }
	    /* must call mysql_store_result() before we can issue any
	     * other query calls
	     */
	    result = mysql_store_result(connection);
	    printf("Rows: %d\n", mysql_num_rows(result));
	    /* process each row in the result set */
	    while( ( row = mysql_fetch_row(result)) != NULL ) {
	        printf("id: %s, val: %s\n",
	               (row[0] ? row[0] : "NULL"),
	               (row[1] ? row[1] : "NULL"));
	    }
	    /* free the result set */
	    mysql_free_result(result);
	    /* close the connection */
	    mysql_close(connection);
	    printf("Done.\n");
      return 1;
}

To compile this code with Eclipse is necessary to add the configuration parameters :

for GCC C++ Compiler in Eclipse settings is necessary to add :
-I/usr/include/mysql -O0 -g3 -c -fmessage-length=0

where
/usr/include/mysql - is the path to the mysql.h

eclipse_compile_with_options_01

eclipse_compile_with_options_02

for the GCC Linker Options is necessary to add the configuration parameters :
-L/usr/lib64/mysql -Xlinker -lmysqlclient_r

where
-L/usr/lib64/mysql - is the path to the mysql library objects.

eclipse_compile_with_options_03

eclipse_compile_with_options_04

eclipse_compile_with_options_05

eclipse_compile_with_options_06

Also you can compile your source code using the necessary mysql library with the followings commandes :

g++ your_program.cpp -I/usr/include/mysql -L/usr/lib/mysql -lmysqlclient_r

or

g++ -o your_program $(mysql_config --cflags) your_program.cpp $(mysql_config --libs)

Comments are closed.

Post Navigation