c語言如何連結到資料庫
轉載自:http://abirdofparadise.blog.hexun.com/8900253_d.html
#include <mysql/mysql.h>
#include <stdio.h>
void main(){
MYSQL *mysql;
MYSQL_RES *res;
MYSQL_ROW row;
char *query;
int t,r;
mysql_init(mysql);
if (!mysql_real_connect(mysql,"localhost","mysql",
"mysql","deneme",0,NULL,0))
{
printf( "Error connecting to database: %s/n",mysql_error(mysql));
}
else printf("Connected.../n");
query="select * from Deneme";
t=mysql_real_query(mysql,query,(unsigned int) strlen(query));
if (t)
{
printf("Error making query: %s/n",
mysql_error(mysql));
}
else printf("Query made.../n");
res=mysql_use_result(mysql);
for(r=0;r<=mysql_field_count(mysql);r++){
row=mysql_fetch_row(res);
if(row<0) break;
for(t=0;t<mysql_num_fields(res);t++){
printf("%s ",row[t]);
}
printf("/n");
}
mysql_close(mysql);
}
簡介
C APIs包含在mysqlclient庫檔案當中與MySQL的原始碼一塊發行,用於連線到資料庫和執行資料庫查詢。有一些例子在MySQL原始碼的clients目錄裡。
MySQL C 變數型別
以下變數型別在MySQL的庫當中定義。我們需要這些變數是為了使用MySQL的函式。這些變數有詳細的解釋,但是這些解釋對於寫程式碼來說並不重要。
MYSQL
以下程式碼塊是用來連線資料庫的通訊過程
typedef struct st_mysql {
NET net; /* Communication parameters */
gptr connector_fd; /* ConnectorFd for SSL */
char *host,*user,*passwd,*unix_socket,
*server_version,*host_info,*info,*db;
unsigned int port,client_flag,server_capabilities;
unsigned int protocol_version;
unsigned int field_count;
unsigned int server_status;
unsigned long thread_id; /* Id for connection in server */
my_ulonglong affected_rows;
my_ulonglong insert_id; /* id if insert on table with NEXTNR */
my_ulonglong extra_info; /* Used by mysqlshow */
unsigned long packet_length;
enum mysql_status status;
MYSQL_FIELD *fields;
MEM_ROOT field_alloc;
my_bool free_me; /* If free in mysql_close */
my_bool reconnect; /* set to 1 if automatic reconnect */
struct st_mysql_options options;
char scramble_buff[9];
struct charset_info_st *charset;
unsigned int server_language;
} MYSQL;
MYSQL_RES
這段程式碼返回查詢結果的行。返回的資料稱為“資料集”
typedef struct st_mysql_res {
my_ulonglong row_count;
unsigned int field_count, current_field;
MYSQL_FIELD *fields;
MYSQL_DATA *data;
MYSQL_ROWS *data_cursor;
MEM_ROOT field_alloc;
MYSQL_ROW row; /* If unbuffered read */
MYSQL_ROW current_row; /* buffer to current row */
unsigned long *lengths; /* column lengths of current row */
MYSQL *handle; /* for unbuffered reads */
my_bool eof; /* Used my mysql_fetch_row */
} MYSQL_RES;
MYSQL_ROW
這個結構是資料行的一個安全表示法。你無法使用以空字元結束的串,因為資料在這個串可以是二進位制, 也許沒有包括任何字元。
typedef struct st_mysql_field {
char *name; /* Name of column */
char *table; /* Table of column if column was a field */
char *def; /* Default value (set by mysql_list_fields) */
enum enum_field_types type; /* Type of field. Se mysql_com.h for types */
unsigned int length; /* Width of column */
unsigned int max_length; /* Max width of selected set */
unsigned int flags; /* Div flags */
unsigned int decimals; /* Number of decimals in field */
} MYSQL_FIELD;
my_ulonglong
該型別用於行數,mysql_affected_rows() 、mysql_num_rows()和mysql_insert_id() 。該型別提供範圍0 到1.84.e19 的支援。在一些系統, 試圖打印出my_ulonglong型別的值是不行的.要顯示這樣的值, 使用%lu printf 格式,把它轉換成unsigned long型別就行了。例如:
printf(Number of rows: %lu/n", (unsigned long) mysql_num_rows(result));
typedef unsigned long my_ulonglong;
連線MySQL,查詢資料
現在假設MySQL已安裝, 使用者和資料表在資料庫被創造。以防有什麼不明問題的情況, 請參考www.mysql.com 網站。
前面已經說過,MySQL的庫檔案在mysqlclient。因此在編譯MySQL程式的時候有必要加上-lmysqlclient編譯選項。MySQL的標頭檔案在/usr/include/mysql目錄下(根據Linux的發行版本的不同,這個目錄也有所不同),因此你的程式頭部看起來有點這個樣子:
#include <mysql/mysql.h>
MySQL的變數型別和函式都包含在這個標頭檔案當中
然後,我們需要建立連線資料庫的變數,可以簡單地這麼做:
MYSQL *mysql;
在連線資料庫之前,我們要呼叫以下函式初始化這個變數:
mysql_init(MYSQL *mysql)
然後
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 int clientflag);
該函式被呼叫連線到資料庫。host是MySQL伺服器的主機名,user是登入的使用者名稱,passwd是登入密碼,db是要連線的資料庫,port是MySQL伺服器的TCP/IP埠,unix_socket是連線型別,clientflag是MySQL執行成ODBC資料庫的標記。在這篇文章當中該標記設成0,連線尋建立後,這個函式返回0。
現在可以連線資料庫,進行查詢了:
char *query;
使用這個字串我們可以創立任何SQL查詢語句進行查詢。執行這個查詢的函式是:
int STDCALL mysql_real_query(MYSQL *mysql,
const char *q,
unsigned int length);
mysql是我們前面用過的變數,q是SQL查詢語句,length是這個查詢語句的長度。如果查詢成功,函式返回0。
查詢之後,我們要到一個MYSQL_RES變數來使用查詢的結果。以下這行創立這個變數:
MYSQL_RES *res;
然後
mysql_use_result(MYSQL *query)
該函式讀出查詢結果。
儘管可以很容易地查詢了,要用這個查詢的結果還要用到其它的函式。第一個是:
MYSQL_ROW STDCALL mysql_fetch_row(MYSQL_RES *result);
該函式把結果轉換成“陣列”。你可能注意到了,該函式返回的是MYSQL_ROW變數型別。以下語句創立那樣的變數:
MYSQL_ROW row;
如前所解釋的,變數row是一個字串陣列。也就是說,row[0]是陣列的第一個值,row[1]是陣列的第二個值...當我們用mysql_fetch_row的時候,接著變數row會取得結果的下一組的資料。當到了結果的尾部,該函式返回一負值。最後我們要關閉這個連線:
mysql_close(MYSQL *mysql)
一些有用的函式
看看如何取得一個表格的欄位,以下這個函式可能實現這個功能:
unsigned int STDCALL mysql_num_fields(MYSQL *mysql);
這個函式返回表格裡有多少個欄位。
取得“資料集”的數目,用:
my_ulonglong STDCALL mysql_num_rows(MYSQL_RES *res);
my_ulonglong STDCALL mysql_affected_rows(MYSQL *mysql);
這個函式是用來得到受INSERT, DELETE, UPDATE查詢語句影響的“資料集”數目。注意該函式返回的資料型別是my_ulonglong