使用c語言訪問mysql

來源:互聯網
上載者:User

用c語言串連MYSQL資料庫包含兩個步驟:
1.初始化一個連結控制代碼結構;
2.實際進行串連
首先,使用mysql_init來初始化串連控制代碼:
#include<mysql.h>
MYSQL *mysql_init(MYSQL *);
通常你傳遞NULL給這個常式,它會返回一個指向新分配的串連控制代碼結構的指標。如果你傳遞一個已有的結構,它將被重新初始化,
這個常式在出錯是返回NULL。
目前為止,你知識分配和初始化一個結構,你還需要使用mysql_real_connect來向一個串連提供參數。
MYSQL *mysql_real_connect(MYSQL *connection , const char *server_host , 
const char *sql_user_name , const char * sql_password ,const char * db_name ,
 unsigned int port_number, const char *unix_socket_name , unsigned int flags);
指標connection必須指向已經被mysql_init初始化過的結構。
server_host既可以是主機名稱,也可以使IP地址。
sql_user_name 和sql_password的含義和它們的字面含義一樣。如果登入名稱為NULL,則假設登入名稱為當前LINUX使用者的登入ID,如果登入
密碼為NULL,你將只能訪問伺服器上無需密碼就可以訪問的資料。
port_number和unix_socket_name應該分別是0和NULL,除非你改變了MYSQL安裝的預設設定。它們將預設使用合適的值。
最後,flags參數用來對一些定義的位元模式進行OR操作。
如果無法串連,它將返回NULL,mysql_error函數可以提供有協助的資訊。
使用完串連之後,通常程式退出時,你要像下面這樣調用函數mysql_close();
void mysql_close(MYSQL *connection);
這將關閉串連。
mysql_option常式(僅能在mysql_init和mysql_real_connect之間調用)可以設定一些選項。
int mysql_option(MYSQL *connection , enum option_to_set , const char *argument);
因為mysql_option一次只能設定一個選項,所以設定一個選項就得調用它一次。
enum選項                                                實際參數類型                                        說明
MYSQL_OPT_TIMEOUT                       const unsigned int *                           連線逾時之前等待的秒數
MYSQL_OPT_COMPRESS                  None,使用NULL                                 網路連接中使用壓縮機制
MYSQL_OPT_COMMAND                    const char *                                         每次串連建立後發送的命令

執行sql語句
執行sql語句的主要API函數被恰當的命名為:
int mysql_query(MYSQL *connection ,const char *query);
如果成功返回0.
1.不返回資料的SQL語句
my_ulonglong mysql_affected_rows(MYSQL *connection);
傳回值是無符號長整型,printf列印時,我們推薦使用%lu將其轉換為無符號長整型。返回的是受之前的sql語句影響的行數。
 2.返回資料的語句
一次提取所有資料的函數
MYSQL_RES *mysql_store_result(MYSQL *connecion);
顯然,你需要在在成功調用mysql_query之後使用這個函數。這個函數將立刻儲存在用戶端中返回的所有資料。
在mysql_store_result調用成功之後,你需要調用mysql_num_rows來得到返回記錄的數目。
my_ulonglong mysql_num_rows(MYSQL_RES *RESULT);
這個函數接受由mysql_store_result返回的結構結構,並返回結構集中的行數。
MYSQL_ROW mysql_fetch_row(MYSQL_RES *result);
這個函數從使用mysql_store_result得到的結果結構中提取一行,並把它放到一個行結構中。當資料用完或發生錯誤時返回
NULL。
void mysql_data_seek(MYSQL_RES *result , my_ulonglong offset);
這個函數用來在結果集中進行跳轉,設定將會被下一個mysql_fetch_row操作返回的行。參數offset的值是一個行號,它必須在
0到結果集總行數減1的範圍內、
MYSQL_ROW_OFFSET mysql_row_tell(MYSQL_RES *result);
這個函數返回一個位移值,它用來表示結果集中的當前位置。它不是一個行號,你不能把它用於mysql_data_seek
MYSQL_ROW_OFFSET mysql_row_seek(MYSQL_RES *result , MYSQL_ROW_OFFSET offset);
這將在結果集中移動當前位置,並返回之前的位置。
一次提取一行資料
MYSQL _RES *mysql_use_result(MYSQL *connection);

為了逐行提取資料,我們將要使用mysql_use_result而不是mysql_store_result
MYSQL_RES *mysql_use_result(MYSQL *connection);
mysql_field_count函數提供了一些關於查詢結果的基本資料,它接受連線物件,並返回結果集中的欄位(列)數目

unsigned int mysql_field_count(MYSQL *conneciton);

#include <stdlib.h>#include <stdio.h>#include "mysql.h"MYSQL my_connection;MYSQL_RES *res_ptr;MYSQL_ROW sqlrow;void display_row();int main(int argc, char *argv[]) {   int res;   mysql_init(&my_connection);     if (mysql_real_connect(&my_connection, "localhost", "rick",                                               "secret", "foo", 0, NULL, 0)) {   printf("Connection success\n");      res = mysql_query(&my_connection, "SELECT childno, fname, age FROM children WHERE age > 5");   if (res) {      printf("SELECT error: %s\n", mysql_error(&my_connection));   } else {      res_ptr = mysql_use_result(&my_connection);      if (res_ptr) {         while ((sqlrow = mysql_fetch_row(res_ptr))) {            printf("Fetched data...\n");    display_row();         }         if (mysql_errno(&my_connection)) {            printf("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;}void display_row() {   unsigned int field_count;   field_count = 0;   while (field_count < mysql_field_count(&my_connection)) {      printf("%s ", sqlrow[field_count]);      field_count++;   }   printf("\n");}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.