当前位置:   article > 正文

mysql与c语言_我的C语言连接Mysql之路

my和c怎么配合用的

1、安装好mysql

2、要实现C连接数据库,需要安装数据库连接器(即MySQL Connector/C)

MySQL Connector/C是一个C语言的client库,这个库是为了实现client/server通信的。

MySQL Connector/C是一个独立的替换为装有mysql server的mysql client库

3、要安装MySQL Connector/C,需要安装cmake。

下载包:cmake-2.6.4.tar.gz

mysql-connector-c-6.0.2-linux-glibc2.3-x86-32bit.tar.gz

可能是因为我的英文比较烂吧,没看懂是怎么个编译过程,我就采用了一种比较笨的方法,如下。

#3.1 解压两个压缩包:

[root@localhost ...]#tar zxvf cmake-2.6.4.tar.gz

[root@localhost ...]#tar zxvf mysql-connector-c-6.0.2-linux-glibc2.3-x86-

32bit.tar.gz

#3.2 将解压后的cmake-2.6.4中的所有内容拷贝到mysql-connector-c-6.0.2-linux-

glibc2.3-x86-32bit中

#3.3 进入mysql-connector-c-6.0.2-linux-glibc2.3-x86-32bit下,编译:

[root@localhost ...]#./bootstrap (编译cmake)

[root@localhost ...]#make

[root@localhost ...]#make install

[root@localhost ...]#cmake -G "Unix Makefiles"

[root@localhost ...]#make

[root@localhost ...]#make install

4、到mysql-connector-c-6.0.2-linux-glibc2.3-x86-32bit目录下

将编译好MySQL Connector/C的/bin、/lib和/include拷贝

到/usr/bin、/usr/lib、/usr/include(这样在包含头文件时,就可以直接写成#include

"mysql.h")

[root@localhost ...]#cp -r /tmp/mysql-connector-c-6.0.2-linux-glibc2.3-x86-

32bit/bin/   /usr/bin/

[root@localhost ...]#cp -r /tmp/mysql-connector-c-6.0.2-linux-glibc2.3-x86-

32bit/lib/   /usr/lib/

[root@localhost ...]#cp -r /tmp/mysql-connector-c-6.0.2-linux-glibc2.3-x86-

32bit/include/   /usr/include/

5、编写*.c连接数据库程序。

例如:test.c

#include

#include

#include

#include "mysql.h"

int main(void)

{

MYSQL *conn_ptr;

MYSQL_RES *res;

MYSQL_ROW row;

char *host = "127.0.0.1";

char *user = "root";

char *password = "";

char *db = "mysql";

unsigned int port = 0;

char *unix_socket = NULL;

unsigned long client_flag = 0;

conn_ptr = mysql_init(NULL);if(!conn_ptr)

{

fprintf(stderr, "init mysql failed\n");

return(-1);

}

conn_ptr = mysql_real_connect(conn_ptr, host, user, password, db, port, unix_socket, client_flag);

if(conn_ptr)

{

printf("Connection success......\n");

}

else

{

fprintf(stderr, "Connection failed......%d:%s\n", errno, strerror(errno));

}

if( mysql_query(conn_ptr, "select host,user from user"))

{

fprintf(stderr, "call mysql_query failed......%d:%s\n",errno, strerror(errno));

}

res = mysql_use_result(conn_ptr);

fprintf(stdout, "select host, user from user talbe in the mysql database:\n");

while((row = mysql_fetch_row(res)))

{

fprintf(stdout, "%s\t%s\n", row[0], row[1]);

}

mysql_free_result(res);

mysql_close(conn_ptr);

return 0;

}

6、编译连接test.c

[root@localhost ...]#gcc test.c -Wall -I/usr/include  -L/usr/lib  -lmysqlclient -o

test

在运行程序之前,一定要开启mysql服务

[root@localhost ...]#/etc/init.c/mysqld start

[root@localhost ...]# ./test

Connection success......

select host, user from user talbe in the mysql database:

127.0.0.1       root

localhost

localhost       root

*好了,大功告成,努力了很长时间才弄明白是怎么回事,如果有帮到你们,我也很开心,嘻嘻……*

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/614119
推荐阅读
相关标签
  

闽ICP备14008679号