mysql c语言教程,C语言调用mysql快速教程(精华篇).pdf

C语言调用mysql快速教程(精华篇).pdf

,使用 语言操作 之前,先在 里头创建一个数据库,一个表,在表里头添加

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

///

/* insert.c */

#include #include #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 successn" ; res mysql_query &my_connection, "insert into children values 10,'Ann',5 " ; if !res printf "Inserted %lu rowsn", unsigned long mysql_affected_rows &my_connection ;

/*里头的函数返回受表中影响的行数*/ else //分别打印出错误代码及详细信息 fprintf stderr, "Insert error %d: %sn",mysql_errno &my_connection ,mysql_error &my_connection ; mysql_close &my_connection ; else fprintf stderr, "Connection failedn" ; if mysql_errno &my_connection fprintf stderr, "Connection error %d: %sn",mysql_errno &my_connection ,mysql_error &my_connection ; return EXIT_SUCCESS; /

代码写完了,要编译哦

#gcc -o insert insert.c -L /usr/local/mysql/lib/mysql/*.a -lz

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 " ;

即可

上面语句实现的功能是,把编

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值