赞
踩
1、C与Mysql
因为Mysql是用C语言开发的,所以会有一系列的API可以调用;
2、C调用Mysql的基础模型#include
#include
#include
#include
int main(void){
int ret = 0;
MYSQL mysql;
MYSQL *connect = NULL;
connect = mysql_init(&mysql); //初始化
if(connect == NULL){
ret = -1;
printf("func mysql_init() err\n");
return ret;
}
connect = mysql_real_connect(connect, "localhost", "root", "123456", "mydb1", 0, NULL, 0);
if(connect == NULL){ //连接mysql
ret = -1;
printf("func mysql_real_connect() err\n");
return ret;
}
printf("func mysql_real_connect() ok\n");
mysql_close(&mysql);
printf("hello world\n");
return ret;
运行命令:
gcc dm01_hello.c -o dm01_hello -I/usr/include -L/usr/lib64/mysql -lmysqlclient -lm -lrt -ldl -lstdc++ -lpthread
运行结果:
3、C查询Mysql#include
#include
#include
#include
/*
*中文问题
*mysql_query 查询
*mysql_store_result 获取句柄
*
*locate mysql.h 可以查找这个.h文件所在的目录
*
*
*/
int main(void){
int ret = 0;
MYSQL mysql;
MYSQL *connect = NULL;
connect = mysql_init(&mysql);
if(connect == NULL){
ret = mysql_errno(&mysql);
printf("func mysql_init() err\n");
return ret;
}
connect = mysql_real_connect(connect, "localhost", "root", "123456", "mydb1", 0, NULL, 0);
//中文问题的解决
mysql_set_character_set(&mysql, "utf8");
if(connect == NULL){
ret = mysql_errno(&mysql);
printf("func mysql_real_connect() err\n");
return ret;
}
//查询
const char *query = "select * from student";
ret = mysql_query(&mysql, query);
if(ret != NULL){
ret = mysql_errno(&mysql);
printf("func mysql_query() err\n");
return ret;
}
//获取结果集和
//结果集和中可能含有多行数据,获取结果集
//mysql_store_result设计理念:告诉句柄,我一下子全部把数据从服务器端取到客户端,然后缓存起来
MYSQL_RES *result = mysql_store_result(&mysql);
//使用的过程中从服务器端获取结果
//MYSQL_RES *result = mysql_use_result(&mysql);
//可得该数据库中这张表每行有多少元素
unsigned int num = mysql_field_count(&mysql);
int i;
MYSQL_ROW row = NULL; //在mysql.h中可以看到
//打印表头
MYSQL_FIELD *fields = mysql_fetch_fields(result);
for(i = 0; i
printf("%s\t", fields[i].name);
}
printf("\n");
//打印表中内容
while(row = mysql_fetch_row(result)){
for(i = 0; i
printf("%s\t", row[i]);
}
printf("\n");
}
/*
* 这里是我们自己看到该表一行有多少元素
while(row = mysql_fetch_row(result)){
printf("%s, %s, %s, %s, %s, %s\n", row[0], row[1], row[2], row[3], row[4], row[5]);
}
*/
mysql_free_result(result);
mysql_close(&mysql);
printf("hello world\n");
return ret;
}
运行结果:
4、C开发Mysql客户端
只实现了查询的功能:#include
#include
#include
#include
int main(int argc, char **argv){
int ret = 0;
MYSQL mysql;
MYSQL *connect = NULL;
char sqlbuf[80];
connect = mysql_init(&mysql);
if(connect == NULL){
ret = mysql_errno(&mysql);
printf("func mysql_init() err\n");
return ret;
}
connect = mysql_real_connect(connect, "localhost", "root", "123456", argv[1], 0, NULL, 0);
//中文问题的解决
mysql_set_character_set(&mysql, "utf8");
if(connect == NULL){
ret = mysql_errno(&mysql);
printf("func mysql_real_connect() err\n");
return ret;
}
for(;;){
memset(sqlbuf, 0, sizeof(sqlbuf));
printf("mysql> :");
//scanf()语句对tab 空格 回车 都省去了,对sql语句将会发生截断,用gets()可保持sql语句的原样性
gets(sqlbuf);
//退出
if(strncmp("exit", sqlbuf, 4) == 0 || strncmp("quit", sqlbuf, 4) == 0){
break;
}
//查询是否为SQL语句
//ret = mysql_query(&mysql, "set name utf8");
ret = mysql_query(&mysql, sqlbuf);
if(ret != NULL){
ret = mysql_errno(&mysql);
printf("func mysql_query() err\n");
return ret;
}
if(strncmp("select", sqlbuf, 6) == 0 || strncmp("SELECT", sqlbuf, 6) == 0){
MYSQL_RES *result = mysql_store_result(&mysql);
unsigned int num = mysql_field_count(&mysql); //表头有多少列
int i;
MYSQL_ROW row = NULL; //在mysql.h中可以看到
//打印表头
MYSQL_FIELD *fields = mysql_fetch_fields(result);
for(i = 0; i
printf("%s\t", fields[i].name);
}
printf("\n");
//打印表中内容
while(row = mysql_fetch_row(result)){
for(i = 0; i
printf("%s\t", row[i]);
}
printf("\n");
}
mysql_free_result(result);
}
}
mysql_close(&mysql);
printf("hello world\n");
return ret;
}
看看mysql.h文件:
可以知道:MYSQL_ROW的真实类型:char **;
运行结果:
看看Mysql:
由于客户端的C语言开发数据库,我只实现了查询功能,其他的功能没有实现,导致没有打印出来,但是现在已经可以通过这个客户端对数据库进行操作了;
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。