mysql c语言教程,C语言调用mysql快速教程(精华篇).pdf
C語(yǔ)言調(diào)用mysql快速教程(精華篇).pdf
,使用 語(yǔ)言操作 之前,先在 里頭創(chuàng)建一個(gè)數(shù)據(jù)庫(kù),一個(gè)表,在表里頭添加
1 c mysql mysql
數(shù)據(jù)如下:
創(chuàng)建數(shù)據(jù)庫(kù),庫(kù)名為 cusemysql:
mysql create database cusemysql;
創(chuàng)建表 表名為 , :
mysql use cusemysql;
mysql create table children childno int not null unique,fname varchar 20 ,age int ;
添加一點(diǎn)數(shù)據(jù)哦:
mysql insert into children values 5, 花 ",10 );
對(duì)拉,為了方便起見(jiàn),把表的大致樣子給大家看看
childno fname age 小星 1 9 大量 2 15
2 ,下面進(jìn)行具體的操作
插入:insert 好的,我們現(xiàn)編輯一段 c 代碼,取名為 insert.c
///
/* insert.c */
#include #include #include "/usr/local/mysql/include/mysql/mysql.h /*注意哦,上面必須是 mysql.h 的絕對(duì)地址,一般在 mysql 下的 include 目錄下,仔細(xì)看看
你的在哪里?*/
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 ;
/*里頭的函數(shù)返回受表中影響的行數(shù)*/ else //分別打印出錯(cuò)誤代碼及詳細(xì)信息 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; /
代碼寫(xiě)完了,要編譯哦
#gcc -o insert insert.c -L /usr/local/mysql/lib/mysql/*.a -lz
ok,現(xiàn)在我們執(zhí)行看看
#./insert
Connection Success
Inserted 1 rows
year,果然可以,呵呵
不信到 mysql 下看看表 children 中是否多了剛才插入的那一行數(shù)據(jù)
注:也許你會(huì)問(wèn)上面 gcc 的命令參數(shù)是什么意思阿,其實(shí),我也不太清楚,呵呵
大概是要把 mysql 下的某個(gè)特定庫(kù)包含進(jìn)來(lái),可是我不知道具體是個(gè)什么庫(kù),所以用*.a 全
部包含進(jìn)來(lái)拉
其實(shí)只要包含 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 " ;
即可
上面語(yǔ)句實(shí)現(xiàn)的功能是,把編
總結(jié)
以上是生活随笔為你收集整理的mysql c语言教程,C语言调用mysql快速教程(精华篇).pdf的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Mac下Idea快捷键总结(不断更新)
- 下一篇: C语言中常用的函数