1樓:匿名使用者
mysql c api程式設計步驟
1、首先我們要包含mysql的標頭檔案,並鏈結mysql動態庫。即新增以下語句:
#include // 進行網路程式設計需要winsock2.h
#include
#pragma comment(lib, 「libmysql.lib」)
2、建立mysql變數。如:
mysql mysql;
3、初始化mysql變數。
mysql_init(&mysql);
4、呼叫mysql_real_connect函式連線mysql資料庫。mysql_real_connect函式的原型如下:
mysql * stdcall mysql_real_connect(mysql *mysql, const char *host,const char *user,const char *passwd,const char *db,unsigned int port,const char *unix_socket,unsigned long clientflag);
引數說明:mysql–前面定義的mysql變數;host–mysql伺服器的位址;user–登入使用者名稱;passwd–登入密碼;db–要連線的資料庫;port–mysql伺服器的tcp服務埠;unix_socket–unix連線方式,為null時表示不使用socket或管道機制;clientflag–mysql執行為odbc資料庫的標記,一般取0。連線失敗時該函式返回0。
5、呼叫mysql_real_query函式進行資料庫查詢。mysql_real_query函式的原型如下:
int stdcall mysql_real_query(mysql *mysql, const char *q, unsigned long length);
引數說明:mysql–前面定義的mysql變數;q–sql查詢語句;length–查詢語句的長度。
查詢成功則該函式返回0。
6、通過呼叫mysql_store_result或mysql_use_result函式返回的mysql_res變數獲取查詢結果資料。
兩個函式的原型分別為:
mysql_res * stdcall mysql_store_result(mysql *mysql);
mysql_res * stdcall mysql_use_result(mysql *mysql);
這兩個函式分別代表了獲取查詢結果的兩種方式。第一種,呼叫mysql_store_result函式將從mysql伺服器查詢的所有資料都儲存到客戶端,然後讀取;第二種,呼叫mysql_use_result初始化檢索,以便於後面一行一行的讀取結果集,而它本身並沒有從伺服器讀取任何資料,這種方式較之第一種速度更快且所需記憶體更少,但它會繫結伺服器,阻止其他執行緒更新任何表,而且必須重複執行mysql_fetch_row讀取資料,直至返回null,否則未讀取的行會在下一次查詢時作為結果的一部分返回,故經常我們使用mysql_store_result。
7、呼叫mysql_fetch_row函式讀取結果集資料。
上述兩種方式最後都是重複呼叫mysql_fetch_row函式讀取資料。mysql_fetch_row函式的原型如下:
mysql_row stdcall mysql_fetch_row(mysql_res *result);
引數result就是mysql_store_result或mysql_use_result的返回值。
該函式返回mysql_row型的變數,即字串陣列,假設為row,則row〔i〕為第i個字段的值。當到結果集尾部時,此函式返回null。
8、結果集用完後,呼叫mysql_free_result函式釋放結果集,以防記憶體洩露。mysql_free_result函式的原型如下:
void stdcall mysql_free_result(mysql_res *result);
9、不再查詢mysql資料庫時,呼叫mysql_close函式關閉資料庫連線。mysql_close函式的原型為:
void stdcall mysql_close(mysql *sock);
2樓:大別山的蝸牛
1、新增標頭檔案路徑(mysql安裝路徑中的include路徑)
2、新增庫檔案(直接從mysql安裝路徑中copy libmysql.lib即可)
3、程式設計運算元據庫
**// accesstomysql.cpp : 定義控制台應用程式的入口點。
//#include "stdafx.h"
#include
#include
#pragma comment(lib,"libmysql.lib")
mysql mysql;
mysql_res* result;
mysql_row row;
int main(void)
//construct the query sql statements
char* sql="select * from student where name='";
char dest[100]=;
strcat(dest,sql);
printf("please enter the student name:");
char name[10]=;
gets(name);
strcat(dest,name);
strcat(dest,"'");
//excute the sql statements
if(mysql_query(&mysql,dest))
//deal with the result
if(mysql_num_rows(result))
}//release the resource
mysql_free_result(result);
mysql_close(&mysql);
system("pause");
return 0;}
3樓:讓公尺煙平惠
mysql也是c\c++的,,,,一開始就是c、c++使用的,官方有文件的
~~~~~
安裝的mysql裡面有標頭檔案.h、和庫檔案.lib
怎樣在c++中呼叫mysql資料庫中的資料
4樓:匿名使用者
建立乙個空的控制台程式,建立乙個cpp檔案,在其中加入如下**。**部分:
cpp**
#include
//定義socket
#include
#include"mysql.h"
//#pragma comment( lib, "libmysql.lib");
//此句話和在附加依賴項中增加libmysql.lib 的功能一樣
usingnamespace std;
int main(char **args)
else
} 相關設定:
1、附加包含標頭檔案的目錄,include就是mysql-5.0.27-win32\include資料夾。
2、附加庫目錄,mysql lib中的檔案就是mysql-5.0.27-win32\lib\opt中的檔案
3、附加依賴項,名稱為libmysql.lib
4、將libmysql.dll拷貝到debug資料夾中,libmysql.dll在lib資料夾中有
5樓:匿名使用者
include "mysql.h"
然後呼叫mysql api庫
linux gcc mysql 如何在c語言中使用嵌入式sql程式設計? 要什麼標頭檔案?如何編譯?越詳細越好 20
6樓:我要鬥爭到死
最起碼包含mysql.h
例項**:
#include
#include
#include "mysql.h"
int main(int argc, char *ar**)conn_ptr = mysql_real_connect(conn_ptr, "localhost", "rick", "secret",
"foo", 0, null, 0);//連線資料庫if (conn_ptr) else
mysql_close(conn_ptr);
return exit_success;
} 編譯:(假定上面檔案取名 con.c,在當前目錄下)gcc -i/usr/include/mysql con.
c -l/usr/lib/mysql -lmysqlclient -o con
C語言讀入資料的問題,C語言讀取資料問題,請大神幫我看看,讀取byte某幾位
能把所有的源 放上來嗎 關於c語言scanf語句無法讀取資料的問題 scanf lf p 是小寫的字母l,不是數字1 關於c語言中用scanf函式如何輸入資料的問題 如果scanf中 d是連著寫的如 d d d 在輸入資料時,資料之間不可以加逗號,只能是空格或tab鍵或專者回車鍵 2 3 4 2 按...
易語言如何讀取Access資料庫中多個字段 表名 條件
暨誠李羅 連線access資料庫 版本 2.支援庫 edb.如果 資料庫連線.連線access mdb資料庫,真 記錄集.置連線 資料庫連線 排序條件 字段,表名,條件 如果 記錄集.開啟並排序 mdb資料表,排序條件 真 否則銷毀 填充樹形框字段到父專案 版本 2.支援庫 edb.支援庫 iext...
select標籤讀取資料庫中的資料
我不知道你的分類的儲存方式是怎麼樣的 我可以給你說一下思路 比較簡單的就是。你儲存文章的時候肯定會有個字段儲存的是文章的分類 可能是分類的id或者直接就是分類的名稱 你用select顯示的時候 第乙個option 就是呼叫當前文章的分類字段,下面的顯示你想選擇的就可以 這個方式很簡單 希望可以幫到你...