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 " ;
即可
上面語句實現的功能是,把編
總結
以上是生活随笔為你收集整理的mysql c语言教程,C语言调用mysql快速教程(精华篇).pdf的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Mac下Idea快捷键总结(不断更新)
- 下一篇: mysql和web文件夹_Linux使用