作者:kyako
1,[比较详细]在 C 里嵌入 SQL: http://www.pgsqldb.org/pgsqldoc-7.4/ecpg.html
2,[在MySQL数据库中使用C执行SQL语句]: http://www.dvbbs.net/tech/data/2006031818989.asp
3,MySQL客户工具和API: http://www.yesky.com/imagesnew/software/mysql/manual_Clients.html
4,基于mysql的高性能数据库应用开发: http://cache.baidu.com/c?word=mysql%3B%5F%3Breal%3B%5F%3Bconnect%2C%B2%CE%CA%FD&url=http%3A//www%2Edaima%2Ecom%2Ecn/Info/76/Info27780/&b=0&a=2&user=baidu
大家一起来开始练习罗
注:下面的所有例子在mandriva linux下测试通过
1,使用c语言操作mysql之前,先在mysql里头创建一个数据库,一个表,在表里头添加数据如下:
创建数据库,库名为cusemysql:
mysql>create database cusemysql;
创建表,表名为:
mysql>use cusemysql;
mysql>create table children(childno int not null unique,fname varchar(20),age int);
添加一点数据哦:
mysql>insert into children values(5,"花儿",10);
对拉,为了方便起见,把表的大致样子给大家看看
childno fname age
1 小星 9
2 大量 15
2 ,下面进行具体的操作
插入:insert
好的,我们现编辑一段c代码,取名为insert.c
Code:
/* insert.c */
#include <stdio.h>
#include <stdlib.h>
#include "/usr/local/mysql/include/mysql/mysql.h"
/*注意哦,上面必须是mysql.h的绝对地址,一般在mysql下的include目录下,仔细看看你的在哪里?*/
int main(int argc, char *argv[])
{
MYSQL my_connection;
int res;
mysql_init(&my_connection);
/*mysql_real_connect(&mysql,host,user,passwd,dbname,0,NULL,0) == NULL)*/
if (mysql_real_connect(&my_connection, "localhost", "root", "","cusemysql",0,NULL,CLIENT_FOUND_ROWS)) {
printf("Connection success/n");
res = mysql_query(&my_connection, "insert into children values(10,'Ann',5)");
if (!res) {
printf("Inserted %lu rows/n",(unsigned long)mysql_affected_rows(&my_connection)); /*里头的函数返回受表中影响的行数*/
} else {
fprintf(stderr, "Insert error %d: s/n",mysql_errno,(&my_connection),mysql_error(&my_connection));
}
mysql_close(&my_connection);
} else {
fprintf(stderr, "Connection failed/n");
if (mysql_errno(&my_connection)) {
fprintf(stderr, "Connection error %d: %s/n",
mysql_errno(&my_connection), mysql_error(&my_connection));
}
}
return EXIT_SUCCESS;
}
[Ctrl+A Select All]
代码写完了,要编译哦
#gcc -o insert insert.c -L /usr/local/mysql/lib/mysql/*.a -lz
如果上边的编译不成功,可以试一下下边的
#gcc -o insert insert.c `mysql_config --cflags --libs`
ok,现在我们执行看看
#./insert
Connection Success
Inserted 1 rows
year,果然可以,呵呵
不信到mysql下看看表children中是否多了刚才插入的那一行数据
注:也许你会问上面gcc的命令参数是什么意思阿,其实,我也不太清楚,呵呵
大概是要把mysql下的某个特定库包含进来,可是我不知道具体是个什么库,所以用*.a全部包含进来拉
其实只要包含mysqlclient.a就可以,你试试看
更新:update
我们只要把上面的代码中的
res = mysql_query(&my_connection, "insert into children values(10,'Ann',5)");
换成
res = mysql_query(&my_connection, "update children set age=20 where childno<5 ");
即可
上面语句实现的功能是,把编号小于5的所有孩子的年龄全部改成20岁
检索:select
看代码之前,最好是先看蓝色字体的部分[介绍了代码中用到的一些函数的作用]
Code:
/* select.c */
#include <stdio.h>
#include <stdlib.h>
#include "/usr/local/mysql/include/mysql/mysql.h"
int main(int argc, char *argv[])
{
MYSQL my_connection;
MYSQL_RES *res_ptr;
MYSQL_ROW sqlrow;
int res;
mysql_init(&my_connection);
/*mysql_real_connect(&mysql,host,user,passwd,dbname,0,NULL,0) == NULL)*/
if (mysql_real_connect(&my_connection, "localhost", "root", "","cusemysql",0,NULL,CLIENT_FOUND_ROWS)){
printf("Connection success/n");
res = mysql_query(&my_connection, "select childno,fname,age from children where age<20");
if (res) {
printf("SELECT error:%s/n",mysql_error(&my_connection));
} else {
res_ptr=mysql_store_result(&my_connection);
if(res_ptr) {
printf("Retrieved %lu Rows/n",(unsigned long)mysql_num_rows(res_ptr));
while((sqlrow=mysql_fetch_row(res_ptr))) {
printf("Fetched data.../n");
}
if (mysql_errno(&my_connection)) {
fprintf(stderr,"Retrive error:s/n",mysql_error(&my_connection));
}
}
mysql_free_result(res_ptr);
}
mysql_close(&my_connection);
}else{
fprintf(stderr, "Connection failed/n");
if (mysql_errno(&my_connection)) {
fprintf(stderr, "Connection error %d: %s/n", mysql_errno(&my_connection), mysql_error(&my_connection)); }
}
return EXIT_SUCCESS;
}
[Ctrl+A Select All]
上面语句实现的功能是:检索出年龄小于20岁的小孩的信息,不过没有对信息进行任何处理哦
下次我们对数据进行一定的处理
这里介绍上面用到的几个函数:
|
检索并处理[比较全面哦,呵呵]:select
下面是详细的代码:
Code:
/* select1.c */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "/usr/local/mysql/include/mysql/mysql.h"
int main(int argc, char *argv[])
{
MYSQL my_connection;
MYSQL_RES *res_ptr; /*指向检索的结果存放地址的指针*/
MYSQL_ROW sqlrow; /*返回的记录信息*/
MYSQL_FIELD *fd; /*字段结构指针*/
char aszflds[25][25]; /*用来存放各字段名*/
int res; /*执行查询操作后的返回标志*/
int i,j,k;
mysql_init(&my_connection);
/*mysql_real_connect(&mysql,host,user,passwd,dbname,0,NULL,0) == NULL)*/
if (mysql_real_connect(&my_connection, "localhost", "root", "","cusemysql",0,NULL,CLIENT_FOUND_ROWS)){
printf("Connection success/n");
res = mysql_query(&my_connection, "select childno,fname,age from children where age<20");
if (res) {
printf("SELECT error:%s/n",mysql_error(&my_connection));
} else {
res_ptr=mysql_store_result(&my_connection);
if(res_ptr) {
printf("Retrieved %lu Rows/n",(unsigned long)mysql_num_rows(res_ptr)); /*取得各字段名*/
for(i=0;fd=mysql_fetch_field(res_ptr);i++)
strcpy(aszflds<i>,fd->name); /*输出各条记录*/
printf("下面是检索出的各条记录信息:/n");
j=mysql_num_fields(res_ptr);
for(i=0;i<j;i++)
printf("%s/t",aszflds<i>);
printf("/n");
while((sqlrow=mysql_fetch_row(res_ptr))) {
for(i=0;i<j;i++)
printf("%s/t",sqlrow<i>);
printf("/n");
}
if (mysql_errno(&my_connection)) {
fprintf(stderr,"Retrive error:s/n",mysql_error(&my_connection));
}
}
mysql_free_result(res_ptr);
}
mysql_close(&my_connection);
}else{
fprintf(stderr, "Connection failed/n");
if (mysql_errno(&my_connection)) {
fprintf(stderr, "Connection error %d: %s/n", mysql_errno(&my_connection), mysql_error(&my_connection));
}
}
return EXIT_SUCCESS;
}
[Ctrl+A Select All]
【来源】