|
Example of operating MySql with C
mysql_c_exam.c
#Include <stdio.h>
#Include <mysql.h>
Int main(char **args)
{
MYSQL_RES *query_result;
MYSQL_ROW row;
MYSQL *db_handle, mysql;
int query_error;
mysql_init(&mysql);
db_handle=mysql_real_connect(&mysql, "localhost", "root", "12345678", "crm", 0, 0, 0);
if(db_handle==NULL)
{
printf(mysql_error(&mysql));
return 1;
}
query_error=mysql_query(db_handle, "select * from bargain");
if(query_error!=0)
{
printf(mysql_error(db_handle));
return 1;
}
query_result=mysql_store_result(db_handle);
while((row=mysql_fetch_row(query_result))!=NULL)
{
printf("%s %s %s\n",(row[0]?row[0]:"NULL"),(row[1]? row[1]:"NULL"),(row[2]? row [2]:"NULL"));
}
mysql_free_result(query_result);
mysql_close(db_handle);
return 0;
}
2) Compile
[Cnscn@test mysql]$ gcc -o mysql_c_exam mysql_c_exam.c -I /usr/local/mysql/include/mysql -L /usr/local/mysql/lib/mysql -l mysqlclient -lz
3) Execution
[Cnscn@test mysql]$ ./mysql_c_exam
1 060108-1901001 34342432
2 060110-10001
3 060118-101001 34342432222
4 060118-1021013 3434sde
5 060118-10210133 3434sded
6 060118-10001 lkfgksdl2455
7 060124-19001 dfgg555
8 060124-11204 sdfsf |
|