废话少说,直接贴代码
#include <stdlib.h> #include <stdio.h> #include <mysql/mysql.h> #include <syslog.h> MYSQL mysql;
void doQuery() { MYSQL_ROW m_row; MYSQL_RES *m_res; char sql[1024]; sprintf(sql,"select count(*) from my_table"); if(mysql_query(&mysql,sql) != 0) { fprintf(stderr, "mysql_query err: %s",mysql_error(&mysql)); } m_res = mysql_store_result(&mysql); if(m_res==NULL) { fprintf(stderr, "get result err: %s",mysql_error(&mysql)); } if(m_row = mysql_fetch_row(m_res)) { printf("count(*) is %d!\n",atoi(m_row[0])); } mysql_free_result(m_res); }
main() { char host[32] = "my_host"; char user[32] = "my_user"; char passwd[32] = "my_passwd"; char db[32] = "my_db"; if( mysql_init(&mysql) == NULL ) { fprintf(stderr,"Init mysql err!"); return -1; } if (mysql_real_connect(&mysql,host,user,passwd,db,0,NULL,0) == NULL) { fprintf(stderr,"Connect to mysql Error:%s!",mysql_error(&mysql)); return -1; } else { puts("Connect to mysql success!"); } doQuery(); mysql_close(&mysql); return 0; }
|
编译使用命令:在/usr/lib/mysql下搜索mysqlclient库,并使用zlib库:
gcc -g c_mysql.c -L/usr/lib/mysql -lmysqlclient -lz
阅读(3271) | 评论(3) | 转发(1) |