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 <stdio.h>
#include <string.h>
#include <errno.h>
#include "mysql.h"
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "mysql.h"
int main(void)
{
MYSQL *conn_ptr;
MYSQL_RES *res;
MYSQL_ROW row;
{
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;
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 ");
return(-1);
}
if(!conn_ptr)
{
fprintf(stderr, "init mysql failed ");
return(-1);
}
conn_ptr = mysql_real_connect(conn_ptr, host, user, password, db, port, unix_socket, client_flag);
if(conn_ptr)
{
printf("Connection success...... ");
}
else
{
fprintf(stderr, "Connection failed......%d:%s ", errno, strerror(errno));
}
if(conn_ptr)
{
printf("Connection success...... ");
}
else
{
fprintf(stderr, "Connection failed......%d:%s ", errno, strerror(errno));
}
if( mysql_query(conn_ptr, "select host,user from user"))
{
fprintf(stderr, "call mysql_query failed......%d:%s ",errno, strerror(errno));
}
{
fprintf(stderr, "call mysql_query failed......%d:%s ",errno, strerror(errno));
}
res = mysql_use_result(conn_ptr);
fprintf(stdout, "select host, user from user talbe in the mysql database: ");
while((row = mysql_fetch_row(res)))
{
fprintf(stdout, "%s %s ", row[0], row[1]);
}
{
fprintf(stdout, "%s %s ", row[0], row[1]);
}
mysql_free_result(res);
mysql_close(conn_ptr);
return 0;
}
mysql_close(conn_ptr);
return 0;
}
*对连接数据库函数的使用,可以参见http://dev.mysql.com/doc/refman/5.1/en/c-api-functions.html,也可以参见我的博文里的《C连接mysql——常用的函数》*
*对SQL语句的使用,可参见http://www.w3school.com.cn/sql/sql_delete.asp。
*对SQL语句的使用,可参见http://www.w3school.com.cn/sql/sql_delete.asp。
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
*好了,大功告成,努力了很长时间才弄明白是怎么回事,如果有帮到你们,我也很开心,嘻嘻……*