愛上C之:API for Mysql

來源:互聯網
上載者:User

標籤:

庫:/usr/lib64/libmysqlclient.so.#.#...  標頭檔:/usr/lib64/mysql/mysql.h

一、建立串連

  • MYSQL *mysql_init(MYSQL *);  用於初始化串連控制代碼,通常以NULL作為參數,執行成功則返回一個指向新分配的串連控制代碼結構的指標,失敗返回NULL
  • MYSQL *mysql_real_connect(
    • MYSQL *connection,           基於mysql_init初始化的結構指標,串連Mysql伺服器,成功返回connection指標,失敗返回NULL
    • const char *server_host,        server_host可以是主機名稱或IP地址
    • const char *sql_user_name,        Mysql使用者名稱稱
    • const char *sql_password,        Mysql登陸密碼
    • const char *db_name,          目標資料庫名稱
    • unsigned int port_number,         預設0
    • const char *unix_socket_name,      預設NULL
    • unsigned int flags);             預設0,可傳遞CLIENT_FOUND_ROWS等參數,改變預設行為
  • void mysql_close(MYSQL *connection);  關閉串連,釋放資源
  • int mysql_options(MYSQL *connection, enum option_to_set, const cahr *argument);  僅能在mysql_init和mysql_real_connect之間執行,且每次只能設定一個選項,多選項設定須多次調用,成功返回0,失敗返回非0,常用於設定連線逾時時間——使用枚舉項MYSQL_OPT_CONNECT_TIMEOUT

二、錯誤處理

  • unsigned int mysql_errno(MYSQL *connection);    返回錯誤碼
  • char *mysql_error(MYSQL *connection);        返迴文本形式的錯誤資訊

三、執行SQL語句

1、不返回資料的SQL語句

  • int mysql_query(MYSQL *connection, const char *query);    第二個參數接收字串數組,用於包含不帶分號的SQL語句,成功返回0 
  • my_ulonglong mysql_affected_rows(MYSQL *connection);     返回SQL語句實際修改的行數,非匹配到的行數,若沒有行受到影響,則返回0

2、標記插入的位置

  • SQL中的LAST_INSERT_ID()函數用於標記當前用戶端串連的最後插入的由AUTO_INCREMENT產生的ID數字 

3、返回資料的SQL語句

  • MYSQL_RES *mysql_store_result(MYSQL *connection);    一次提取所有資料,失敗返回NULL
  • MYSQL_RES *mysql_use_result(MYSQL *connection);      一次提取一行資料,失敗返回NULL
  • my_ulonglong mysql_num_rows(MYSQL_RES *result);      返回mysql_store/use_result函數提取到的行數,若沒有返回行,則返回0
  • MYSQL_ROW mysql_fetch_row(MYSQL_RES *result);      逐行從mysql_store/use_result返回的結果中提取資料,存放至一個行資料結構體中,用於後續處理,資料用完或發生錯誤時返回NULL
  • void mysql_data_seek(MYSQL_RES *result, my_ulonglong offset);  僅能與mysql_store_result函數搭配使用,用於設定下一次將被mysql_fetch_row函數操作的行號,offset的取值範圍為0至結果集的總行數減1
  • MYSQL_ROW_OFFSET mysql_row_tell(MYSQL_RES *result);     僅能與mysql_store_result函數搭配使用,
  • MYSQL_ROW_OFFSET mysql_row_seek(MYSQL_RES *result, MYSQL_ROW_OFFSET offset);  僅能與mysql_store_result函數搭配使用,
  • void mysql_free_result(MYSQL_RES *resuslt);  清理已操作完成的對象

4、處理返回的資料

  • unsigned int mysql_field_count(MYSQL *connection);  返回提取到的資料集的欄位數目
  • MYSQL_FIELD *mysql_fetch_field(MYSQL_RES *result);  按單一欄位提取中繼資料——表名稱、欄位名稱、欄位資料類型、欄位定義的標誌(如NULL、PRIMARY KEY等) ,存放至一個欄位結構體(包含各項中繼資料)中,資料用完或發生錯誤返回NULL
  • STRUCT MYSQL_FIELD {    char *name;  欄位名稱    char *table;  所屬表的名稱    char *def;    enum enum_field_types type;  欄位類型:FIELD_TYPE_DECIMAL/LONG/STRING/VAR_STRING等    unsigned int length;  定義表時指定的欄位寬度    unsigned int max_length;  僅在使用mysql_store_result時有效,表示結果集中以位元組為單位的最長欄位值的長度    unsigned int flags;  欄位定義的標誌,如:AUTO_INCREMENT_FLAG、BINARY_FLAG等    unsigned int decimals;  小數位元,僅對數字欄位有效;IS_NUM宏可用於判斷欄位類型是否為數字,是則返回true};
  • MYSQL_FIELD_OFFSET mysql_field_seek(MYSQL_RES *result, MYSQL_FIELD_OFFSET offset);  類同mysql_row_seek,用於設定某一行內的欄位位移

四、樣本程式

 

愛上C之:API for Mysql

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.