基本概念
C APIs包含在mysqlclient庫文件當中,與MySQL的源代碼一塊發行,用於連接到資料庫和執行資料庫查詢。
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <dlfcn.h> #include <mysql/mysql.h> 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 :%d\n", 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 :%d\n", ret); return ret; } else { printf("func mysql_real_connect() ok\n"); } mysql_close(&mysql); return ret; }
程式設計步驟
1 透過呼叫mysql_library_init(),初始化MYSQL庫
2 透過呼叫mysql_init()初始化連接處理程序,並透過呼叫mysql_real_connect()連接到伺服器sql
3 發出語句並呼叫mysql_SQL_connect()連接到伺服器
3呼叫mysql_close(),關閉與MYSQL伺服器的連線
5 透過呼叫mysql_library_end(),結束MYSQL庫的使用
編譯需要注意的問題問題1:
[mysql01@localhost dm01]$ gcc -oo dm11_hello[mysql01@localhost dm01]$ gcc -oo dm11_hello. I/usr/include/ -L/usr/lib64/mysql/ -lmysqlclient
/usr/lib64/mysql//libmysqlclient.a(net_serv.cc.o):(.data.DW.ref.__gxx_personality_v0[DW. .__gxx_personality_v0]+0x0): undefined reference to __gxx_personality_v0'
/usr/lib64/mysql//libmysqlclient.a(password.c.o): In functionscramble_323': 編譯
/usr/lib64/mysql//libmysqlclient.a(dso_dlfcn.o): In function dlfcn_globallookup':
dsooo_dlfcn.c:open(textcndx31): (.text+0x44): undefined reference to dlsym'
dso_dlfcn.c:(.text+0x4f): undefined reference todlclose'
/usr/lib64/mysqlined reference todlclose'
/usr :
dso_dlfcn.c:(.text+0xa0): undefined reference todladdr'
dso_dlfcn.c:(.text+0x101): undefined reference to dlerror'
/us/645/sql ): 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 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/sql.58 /mysys/my_thr_init.c:220: undefined reference topthread_mutexattr_destroy'
. dm11_hello dm11_hello.c -I/usr/include/ -L/usr/lib64/mysql/ -lmysqlclient -ldl -lstdc++ -lpthread
/usr/lib64/mysql//libmycom :/pb2/build/sb_0-12734909-1406113305.48/rpm/BUILD/mysqlcom-pro-5.6.20/mysqlcom-pro-5.6.20/mysys/my_getgetsys.c:44:mysqlcom-pro-5.6.20/mysys/my_getgetsysgets:44:mysqlcom-pro-5.6.20/mysys/my_getgetsysgets:44:mysqlcom-freences rewidwect tosed 填頁🜥: 返回。 1
[mysql01@localhost dm01]$
缺少運行時動態庫以及數學庫,添加-lm以及-lrt選項
.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 $< -o $@ hello:hello.o $(CC) $(CFLAGS) $^ $(LFLAGS) -o $@ clean: rm -f *.o $(BIN)以上是MySQLSQL相關內容請關注PHP中文網(www.php.cn)!