mysql可以操作指针么_使用C API操作mysql数据库
使用C API操作mysql數據庫
說明:使用mysql提供的C API編程實現數據庫的最基本操作,算是拋磚引玉吧。如果想了解更多,請安裝mysql(如何安裝可以參考本人的“軟件使用之:mysql服務器安裝和配置”)并仔細閱讀其操作手冊。
例子代碼下載地址:
http://pickup.mofile.com/3413396272029309
一、基本操作步驟:
1、初始化mysql
MYSQL *mysql = mysql_init(NULL);
2、連接數據庫
mysql_real_connect(mysql, HOST, USER, PASSWORD, DATABASE, 0, NULL, 0);
3、執行數據庫操作
mysql_query(mysql, sql);
4、關閉mysql
mysql_close(mysql);
二、執行查詢操作:
相信沒有人會否認查詢是數據庫中最基本、最常用的操作吧,呵呵。
mysql查詢需要在調用mysql_query()后執行其它函數來獲取數據庫表中相應字段的值:
1、存儲操作結果集
MYSQL_RES *mysql_res = mysql_store_result(mysql);
2、獲取結果集中一行記錄
MYSQL_ROW *mysql_row = mysql_fetch_row(mysql_res);
3、通過指針獲取記錄中的值
指針mysql_row[0]指向的就是該條記錄第一列(查詢的第一個字段)的值
4、釋放存儲結果集
mysql_free_result(mysql_res);
三、其它操作:
1、設置查詢返回數據的字符集
#define MYSQL_CHARSET??? "gbk" /* 替換為你想使用的字符集 */
char sql[1024] = {0};
sprintf(sql, "set names %s", MYSQL_CHARSET);
mysql_query(mysql, sql);
2、獲取結果集中列(查詢表的字段)的數目
int num_fields = mysql_num_fields(mysql_res);
四、相關函數說明:
1、初始化函數:用于分配或初始化一個MYSQL對象,返回其句柄。
MYSQL *mysql_init(MYSQL *mysql)
Description
Allocates or initializes a MYSQL object suitable for mysql_real_connect(). If mysql is a NULL pointer, the function allocates, initializes, and returns a new object. Otherwise, the object is initialized and the address of the object is returned. If mysql_init() allocates a new object, it is freed when mysql_close() is called to close the connection.
Return Values
An initialized MYSQL* handle. NULL if there was insufficient memory to allocate a new object.
2、關閉函數:用于關閉數據庫連接,釋放前面分配的MYSQL對象資源。
void mysql_close(MYSQL *mysql)
Description
Closes a previously opened connection. mysql_close() also deallocates the connection handle pointed to by mysql if the handle was allocated automatically by mysql_init() or mysql_connect().
Return Values
None.
3、連接數據庫函數:通過指定主機名(或IP)、用戶、密碼、要操作的數據庫來連接mysql服務器。
MYSQL *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 client_flag)
Description
mysql_real_connect() attempts to establish a connection to a MySQL database engine running on host. mysql_real_connect() must complete successfully before you can execute any other API functions that require a valid MYSQL connection handle structure.
Return Values
A MYSQL* connection handle if the connection was successful, NULL if the connection was unsuccessful. For a successful connection, the return value is the same as the value of the first parameter.
4、SQL語句執行函數:用于執行SQL語句,對數據庫的操作由此函數完成。
int mysql_query(MYSQL *mysql, const char *stmt_str)
Description
Executes the SQL statement pointed to by the null-terminated string stmt_str. Normally, the string must consist of a single SQL statement and you should not add a terminating semicolon (‘;’) or /g to the statement. If multiple-statement execution has been enabled, the string can contain several statements separated by semicolons. See Section 22.2.9, “C API Handling of Multiple Statement Execution”.
mysql_query() cannot be used for statements that contain binary data; you must use mysql_real_query() instead. (Binary data may contain the ‘/0’ character, which mysql_query() interprets as the end of the statement string.)
Return Values
Zero if the statement was successful. Non-zero if an error occurred.
5、存儲結果集函數:存儲sql語句成功返回的結果
MYSQL_RES *mysql_store_result(MYSQL *mysql)
Description
After invoking mysql_query() or mysql_real_query(), you must call mysql_store_result() or mysql_use_result() for every statement that successfully retrieves data (SELECT, SHOW, DESCRIBE, EXPLAIN, CHECK TABLE, and so forth). You must also call mysql_free_result() after you are done with the result set.
Return Values
A MYSQL_RES result structure with the results. NULL (0) if an error occurred.
6、獲取記錄函數:從結果集result中返回一條記錄
MYSQL_ROW mysql_fetch_row(MYSQL_RES *result)
Description
Retrieves the next row of a result set. When used after mysql_store_result(), mysql_fetch_row() returns NULL when there are no more rows to retrieve. When used after mysql_use_result(), mysql_fetch_row() returns NULL when there are no more rows to retrieve or if an error occurred.
The number of values in the row is given by mysql_num_fields(result). If row holds the return value from a call to mysql_fetch_row(), pointers to the values are accessed as row[0] to row[mysql_num_fields(result)-1]. NULL values in the row are indicated by NULL pointers.
The lengths of the field values in the row may be obtained by calling mysql_fetch_lengths(). Empty fields and fields containing NULL both have length 0; you can distinguish these by checking the pointer for the field value. If the pointer is NULL, the field is NULL; otherwise, the field is empty.
Return Values
A MYSQL_ROW structure for the next row. NULL if there are no more rows to retrieve or if an error occurred.
7、釋放存儲的結果集函數:釋放存儲結果所分配的資源
void mysql_free_result(MYSQL_RES *result)
Description
Frees the memory allocated for a result set by mysql_store_result(), mysql_use_result(), mysql_list_dbs(), and so forth. When you are done with a result set, you must free the memory it uses by calling mysql_free_result().
Return Values
None.
8、獲取結果集的列數:
unsigned int mysql_num_fields(MYSQL_RES *result)
To pass a MYSQL* argument instead, use unsigned int mysql_field_count(MYSQL *mysql).
Description
Returns the number of columns in a result set.
Return Values
An unsigned integer representing the number of columns in a result set.
9、獲取當前記錄各字段值的長度:
unsigned long *mysql_fetch_lengths(MYSQL_RES *result)
Description
Returns the lengths of the columns of the current row within a result set. If you plan to copy field values, this length information is also useful for optimization, because you can avoid calling strlen(). In addition, if the result set contains binary data, you must use this function to determine the size of the data, because strlen() returns incorrect results for any field containing null characters.
The length for empty columns and for columns containing NULL values is zero. To see how to distinguish these two cases, see the description for mysql_fetch_row().
Return Values
An array of unsigned long integers representing the size of each column (not including any terminating null characters). NULL if an error occurred.
五、代碼編譯和鏈接:
1、編譯選項:
添加mysql頭文件目錄
-I/usr/local/mysql/include # 通過二進制包安裝的
-I/usr/local/mysql/include/mysql # 通過源代碼包安裝的
添加mysql庫文件目錄
-L/usr/local/mysql/lib # 通過二進制包安裝的
-L/usr/local/mysql/lib/mysql # 通過源代碼包安裝的
2、鏈接選項(分動態鏈接和靜態鏈接來講)
動態鏈接
-lmysqlclient -lm -lz
靜態鏈接
-static -lmysqlclient -lmygcc -lm -lz -lcrypt -lpthread
順便說說mysql的靜態鏈接,我首先使用的用動態鏈接:-lmysqlclient -lm -lz順利通過,export LD_LIBRARY_PATH=/usr/local/mysql/lib后執行成功。接著我是用靜態鏈接:-static -lmysqlclient -lm -lz,出來一堆鏈接錯誤,基本分為:
undefined reference to `__pure_virtual'
undefined reference to `__builtin_delete'
undefined reference to `pthread_self'
其中:pthread_self好辦,肯定是加載pthread線程庫,而pure_virtual和builtin_delete不熟,到網上查了下,沒找到解決方法,倒是留意到有個人說了句二者是c++里的,于是開始懷疑是不是編譯器的問題,到/usr/local/mysql/lib下找了下,發現一個庫,名叫:libmygcc.a,想想是不是鏈接這個庫就可以了呢?試一下,-static -lmysqlclient -lmygcc -lm -lz -lcrypt -lpthread,成功!呵呵,順利完成任務!
所以,有時候做事還是要自己多動動腦筋,多想想點子,多動動手的。
總結
以上是生活随笔為你收集整理的mysql可以操作指针么_使用C API操作mysql数据库的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android设置字体为微软雅黑,安卓开
- 下一篇: 广告联盟介绍之——A5广告联盟