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
///////////////////////////////////
/* 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;
}
/////////////////////////////////////////////
代碼寫完了,要編譯哦
#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 ");
即可
上面語句實現的功能是,把編號小於5的所有孩子的年齡全部改成20歲
檢索:select
看代碼之前,最好是先看藍色字體的部分[介紹了代碼中用到的一些函數的作用]
//////////////////////////////////////////////////
/* 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;
}
//////////////////////////////////////////////////
面語句實現的功能是:檢索出年齡小於20歲的小孩的信息,不過沒有對信息進行任何處理哦
下次我們對數據進行一定的處理
這裏介紹上面用到的幾個函數:
|
檢索並處理[比較全面哦,呵呵]:select
下面是詳細的代碼:
//////////////////
/* 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;
}
//////////////////////////////////////
主要要注意的幾個地方是:
1,mysql.h的路徑,必須確定它的絕對路徑哦(這句話是錯誤的,只要在編譯是把對應庫編譯進去即可,具體操作方法請查詢本博客)
2,注意你的數據庫的用戶名和密碼是否正確
3,在編譯的時候必須包含這個庫文件mysqlclient.a[記不太清楚,不過你可以直接包含它所在的目錄下的所有庫,肯定就沒有問題,
各方意見:
一.
編譯方法有點不規範。
正確的應該是這樣:
1.在程序開頭包含頭文件應這樣寫:
#include <mysql.h>
2.編譯應這樣寫:
$ gcc -o mysql mysql.c `mysql_config --cflags --libs`(我測試了,不可用,估計大的思想是對的)
參考:http://www.blog.edu.cn/user2/45242/archives/2005/1023572.shtml