#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