Rumah > pangkalan data > tutorial mysql > MySQL入门之C语言操作MySQL

MySQL入门之C语言操作MySQL

黄舟
Lepaskan: 2017-01-19 15:49:28
asal
2453 orang telah melayarinya

基本概念

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;
}
Salin selepas log masuk

编程步骤

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++的动态库,在编译选项中添加-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 $<  -o   $@ 
hello:hello.o 
    $(CC) $(CFLAGS) $^  $(LFLAGS) -o  $@ 
clean:
    rm -f *.o $(BIN)
Salin selepas log masuk

以上就是MySQL入门之C语言操作MySQL的内容,更多相关内容请关注PHP中文网(m.sbmmt.com)!


Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan