MySQL入门之C语言操作MySQL

基本概念

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()连接到服务器
3 发出SQL语句并处理其结果
4 通过调用mysql_close(),关闭与MYSQL服务器的连接
5 通过调用mysql_library_end(),结束MYSQL库的使用

编译需要注意的问题

  1. 问题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 function
    scramble_323’:

需要用到c++的动态库,在编译选项中添加-lstdc++选项

  1. 问题2
    /usr/lib64/mysql//libmysqlclient.a(dso_dlfcn.o): In function dlfcn_globallookup':
    dso_dlfcn.c:(.text+0x31): undefined reference to
    dlopen’
    dso_dlfcn.c:(.text+0x44): undefined reference to dlsym'
    dso_dlfcn.c:(.text+0x4f): undefined reference to
    dlclose’
    /usr/lib64/mysql//libmysqlclient.a(dso_dlfcn.o): In function dlfcn_pathbyaddr':
    dso_dlfcn.c:(.text+0xa0): undefined reference to
    dladdr’
    dso_dlfcn.c:(.text+0x101): undefined reference to dlerror'
    /usr/lib64/mysql//libmysqlclient.a(dso_dlfcn.o): In function
    dlfcn_bind_func’:
    dso_dlfcn.c:(.text+0x464): undefined reference to `dlsym’

回调函数的正反向调用,需要使用到dl函数库,在编译选项中添加-ldl选项

  1. 问题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 to
pthread_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 to
pthread_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 to
clock_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)
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值