用c链接mysql

 filename: conmysql.c
 compile command: gcc -o connection -g conmysql.c -lz /home/wangwei/workspace/tcpl/lib/libmysql.so.16.0.0

1
#include <stdio.h>
2 #include <stdlib.h>
3 #include "include/mysql.h"
4 int errorno;
5 int main(int argc, char *argv[]) {
6 MYSQL *conn_ptr;
7 conn_ptr = mysql_init(NULL);
8 if(!conn_ptr) {
9 fprintf(stderr, "mysql_init failed\n");
10 return EXIT_FAILURE;
11 }
12 conn_ptr = mysql_real_connect(conn_ptr, "127.0.0.1", "root", "929971", "test", 0, NULL, 0);
13 if(conn_ptr) {
14 printf("Connected success\n");
15 } else {
16 errorno = mysql_errno(conn_ptr);
17 printf("%d\n", errorno);
18 }
19 mysql_close(conn_ptr);
20 return EXIT_SUCCESS;
21
22 }

posted @ 2011-12-01 01:00  呆头鱼  阅读(245)  评论(0编辑  收藏  举报