基本概念
C APIs包含在mysqlclient庫文件當中,與MySQL的源代碼一塊發行,用于連接到數據庫和執行數據庫查詢。
#include?<stdio.h> #include?<stdlib.h> #include?<string.h> #include?<dlfcn.h> #include?<mysql> int?main() { ????int?????ret?=?0; ????MYSQL???mysql; ????MYSQL???*con?=?NULL; ????con?=?mysql_init(&mysql); ????if?(con?==?NULL) ????{ ????????ret?=?mysql_errno(&mysql); ????????printf("func?mysql_init()?err?:%dn",?ret); ????????return?ret; ????} ????//連接mysql服務器 ????//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)?; ????con?=?mysql_real_connect(&mysql,?"localhost",?"root",?"123456",?"mydb2",?0,?NULL,?0?); ????if?(con?==?NULL) ????{ ????????ret?=?mysql_errno(&mysql); ????????printf("func?mysql_real_connect()?err?:%dn",?ret); ????????return?ret; ????} ????else ????{ ????????printf("func?mysql_real_connect()?okn"); ????} ????mysql_close(&mysql); ????return?ret; }</mysql></dlfcn.h></string.h></stdlib.h></stdio.h>
編程步驟
1 通過調用mysql_library_init(),初始化MYSQL庫?
2 通過調用mysql_init()初始化連接處理程序,并通過調用mysql_real_connect()連接到服務器?
3 發出SQL語句并處理其結果?
4 通過調用mysql_close(),關閉與MYSQL服務器的連接?
5 通過調用mysql_library_end(),結束MYSQL庫的使用
編譯需要注意的問題 問題1:?
[mysql01@localhost dm01]$ gcc -o dm11_hello dm11_hello.c -I/usr/include/ -L/usr/lib64/mysql/ -lmysqlclient?
/usr/lib64/mysql//libmysqlclient.a(net_serv.cc.o):(.data.DW.ref.__gxx_personality_v0[DW.ref.__gxx_personality_v0]+0x0): undefined reference to __gxx_personality_v0′?
/usr/lib64/mysql//libmysqlclient.a(password.c.o): In functionscramble_323’:
立即學習“C語言免費學習筆記(深入)”;
需要用到c++的動態庫,在編譯選項中添加-lstdc++選項
問題2?
/usr/lib64/mysql//libmysqlclient.a(dso_dlfcn.o): In function dlfcn_globallookup’:?
dso_dlfcn.c:(.text+0x31): undefined reference todlopen’?
dso_dlfcn.c:(.text+0x44): undefined reference to dlsym’?
dso_dlfcn.c:(.text+0x4f): undefined reference todlclose’?
/usr/lib64/mysql//libmysqlclient.a(dso_dlfcn.o): In function dlfcn_pathbyaddr’:?
dso_dlfcn.c:(.text+0xa0): undefined reference todladdr’?
dso_dlfcn.c:(.text+0x101): undefined reference to dlerror’?
/usr/lib64/mysql//libmysqlclient.a(dso_dlfcn.o): In functiondlfcn_bind_func’:?
dso_dlfcn.c:(.text+0x464): undefined reference to `dlsym’
回調函數的正反向調用,需要使用到dl函數庫,在編譯選項中添加-ldl選項
問題3
thread_mutex_trylock’?
/usr/lib64/mysql//libmysqlclient.a(my_thr_init.c.o): In function my_thread_global_end’:?
/pb2/build/sb_0-12734909-1406113305.48/rpm/BUILD/mysqlcom-pro-5.6.20/mysqlcom-pro-5.6.20/mysys/my_thr_init.c:214: undefined reference topthread_key_delete’?
/pb2/build/sb_0-12734909-1406113305.48/rpm/BUILD/mysqlcom-pro-5.6.20/mysqlcom-pro-5.6.20/mysys/my_thr_init.c:217: undefined reference to pthread_mutexattr_destroy’?
/pb2/build/sb_0-12734909-1406113305.48/rpm/BUILD/mysqlcom-pro-5.6.20/mysqlcom-pro-5.6.20/mysys/my_thr_init.c:220: undefined reference topthread_mutexattr_destroy’
MySQL的動態庫用到多線程,所以編譯選項中添加-lpthread選項
4.問題4
[mysql01@localhost dm01]$ gcc -o dm11_hello dm11_hello.c -I/usr/include/ -L/usr/lib64/mysql/ -lmysqlclient -ldl -lstdc++ -lpthread?
/usr/lib64/mysql//libmysqlclient.a(my_getsystime.c.o): In function my_getsystime’:?
/pb2/build/sb_0-12734909-1406113305.48/rpm/BUILD/mysqlcom-pro-5.6.20/mysqlcom-pro-5.6.20/mysys/my_getsystime.c:44: undefined reference toclock_gettime’?
collect2: ld 返回 1?
[mysql01@localhost dm01]$
缺少運行時動態庫以及數學庫,添加-lm以及-lrt選項
完整的gcc編譯命令:
gcc -o hello hello.c -I/usr/include/mysql/ -L/usr/lib/i386-linux-gnu/ -lmysqlclient -lm -ldl -lstdc++ -lpthread -lrt
一般Makefile編寫
.PHONY:clean?all CC=gcc CFLAGS=-Wall?-g LFLAGS=-L/usr/lib/i386-linux-gnu/?-lmysqlclient?-ldl?-lpthread?-lm?-lrt?-lstdc++ BIN=hello? all:$(BIN) %.o:%.c ????$(CC)?$(CFLAGS)??-c?$<p>以上就是MySQL入門之C語言操作MySQL的內容,更多相關內容請關注PHP中文網(www.php.cn)!</p>