Mysql C Api
I am not going to introduce mysql database system here. cause it is not the right place , and i cant see a damn thing.
we are using mysql 5.x First of all : before you can do anything with mysql using its api , you have to establish a connection.and before you can establish your connection you need to know which functions are you going to use.and before you can use those functions you need to know which types are you going to use. and before you compile your program that uses mysql c api , you have to provide the mysql metadata to your compiler to recognize where are the libraries/headers which it is going to use
so lets start up with showing you types you will be dealing with :
MYSQL
structure that handles one mysql connection
MYSQL_RES
structure that handles the result of a query
MYSQL_ROW
array of strings that handles the fetched row | beware of using it strings
MYSQL_FIELD
structure that contains information about the fetched field ->name , remember it we are going to use it
before you start , you need to know how to compile mysql api based programs a tool named mysql_config returns the metadata about mysql include files and libraries so your compiler can link it all together. an example
gcc test.c `mysql_config --cflags --libs` -o test_mysql
now , lets start up with a simple source file that establishes a connection with mysql: before you can use your mysql handler you need to initiate it
MYSQL *mysql_init(MYSQL *mysql)
then you need to connect yo your server MYSQL *mysql_real_connect(MYSQL *mysql, const char *host, const char *user, const char *passwd, const char *db, unsigned int port, const char *unix_socket, unsigned long client_flag)
then you need to close this connection :
void mysql_close(MYSQL *mysql)
example:
now that you have connected to a mysql server , and as you can see we are to connected to mysql default database.so lets create our own database: we need to connect to the server , then issue "CREATE DATABASE" query. int mysql_query(MYSQL *mysql, const char *stmt_str)
you will provide it with a mysql connection handler , and the query as a null terminated string , returns 0 if success.
now that we have created a database lets get connected to it , and issue our queries to issue queries int mysql_query(MYSQL *mysql, const char *stmt_str)
returns 0 on success. code example3 lets sum it up , we always start by initiating the MYSQL pointer then we connect to the mysql server using this pointer and select our database
then we call mysql_query and execute our query
- NOTICE: there are some queries that returns a result such as (select,show...) queries that returns something; these queries requires that each time they are executed , their result must be stored or you will get errors.
lets start adding some elements in our table , then execute a query that fetch elements from the table to store the result in a result set as shown in the introduced type above
MYSQL_RES *mysql_store_result(MYSQL *mysql)
to get the number of fields in a result set
unsigned int mysql_num_fields(MYSQL_RES *result)
to fetch rows from the result set
MYSQL_ROW mysql_fetch_row(MYSQL_RES *result)
this function fetches one row at time , so we put it into a loop until it returns NULL , in this case there are no more rows to be fetched
what if you want to print information about the fields also ?
MYSQL_FIELD *mysql_fetch_fields(MYSQL_RES *result)
add this code to your source : code example4
i wrote this in a hurry , cause i just wanted to sleep if you find any mistakes , feel free to correct it.