mysql c api(一)

參考資料:http://dev.mysql.com/doc/refman/5.5/en/c.html

 

近幾天,工作中需要用到MYSQL數據庫編程,開始學習linux下mysql c編程。剛把一個程序寫得七七八八,寫一下總結。

這幾天看得最多的,就是MYSQL的官方文檔了,一邊看文檔,一邊寫程序試着使用裏面提到的接口,熟悉起來還算是比較快的。

 

先說說編程環境吧,操作系統:linux,編譯軟件:g++,庫文件:mysql-connector-c-6.0.2-linux-glibc2.3-x86-64bit

1.下載安裝庫文件。因爲開發的機子中沒有安裝mysql,系統沒有mysql編程需要用到的庫文件,只能自己下載,

在官網http://dev.mysql.com/downloads/connector/c/#downloads找到這個庫文件,下載,解壓,放到系統特定目錄(自己隨便放,知道路徑就行了)。

2.編寫程序。

3.進行編譯。寫完測試程序後,執行,g++ dminingmysql.cpp _public.cpp -o dminingmysql -I(mysql庫文件存放路徑/include) -L(mysql庫文件存放目錄/lib) -lmysqlclient -lz。

就可以運行查看結果了。

 

基本數據結構

MYSQL:數據庫連接句柄,不能COPY,只能使用mysql_init()獲得

MYSQL_RES:用來保存結果集(result set)

MYSQL_FIELD:查看字段屬性(字段名,長度,字段類型......)

 

基本方法

MYSQL *mysql_init(MYSQL *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.

Errors

In case of insufficient memory, NULL is returned.

 

 

 

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):初始完MYSQL後,建立連接。

 

 

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.The first parameter should be the address of an existing MYSQL structure. Before calling mysql_real_connect()you must call mysql_init() to initialize the MYSQL structure. 

 

 

Return Values

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.

 

 

 

unsigned long mysql_get_server_version(MYSQL *mysql):獲取服務器版本

Description

Returns the version number of the server as an integer.

Return Values

A number that represents the MySQL server version in this format:

major_version*10000 + minor_version *100 + sub_version

For example, 5.1.5 is returned as 50105.

This function is useful in client programs for quickly determining whether some version-specific server capability exists.

Errors

None.

 

 

MYSQL_RES *mysql_list_dbs(MYSQL *mysql, const char *wild):列出服務器的數據庫

Description

Returns a result set consisting of database names on the server that match the simple regular expression specified by the wild parameter. wild may contain the wildcard characters “%” or “_”, or may be a NULL pointer to match all databases. Calling mysql_list_dbs() is similar to executing the query SHOW DATABASES [LIKEwild].

You must free the result set with mysql_free_result().

Return Values

MYSQL_RES result set for success. NULL if an error occurred.

 

 

 

MYSQL_FIELD *mysql_fetch_fields(MYSQL_RES *result):獲取查詢的字段信息(長度,字段名,字段類型等等)

Description

Returns an array of all MYSQL_FIELD structures for a result set. Each structure provides the field definition for one column of the result set.

Return Values

An array of MYSQL_FIELD structures for all columns of a result set.

Errors

None.

 

 

 

MYSQL_ROW mysql_fetch_row(MYSQL_RES *result):根據結果集獲得一行記錄信息。

Description

Retrieves the next row of a result set. 

 

 

 

int mysql_query(MYSQL *mysql, const char *stmt_str):執行SQL語句

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.

 

 

 

Return Values

Zero if the statement was successful. Nonzero if an error occurred.

 

 

 

MYSQL_RES *mysql_store_result(MYSQL *mysql):保存執行完mysql_query()的結果集

Description

After invoking mysql_query() or mysql_real_query(), you must call mysql_store_result() ormysql_use_result() for every statement that successfully produces a result set (SELECTSHOWDESCRIBE,EXPLAINCHECK TABLE, and so forth). You must also call mysql_free_result() after you are done with the result set.

You don't have to call mysql_store_result() or mysql_use_result() for other statements, but it does not do any harm or cause any notable performance degradation if you call mysql_store_result() in all cases. You can detect whether the statement has a result set by checking whether mysql_store_result() returns a nonzero value

 

 

 

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().

Do not attempt to access a result set after freeing it.

Return Values

None.

Errors

None.

 

 

 

void mysql_close(MYSQL *mysql):斷開數據庫連接,釋放MYSQL結構體空間

Description

Closes a previously opened connection. mysql_close() also deallocates the connection handle pointed to bymysql if the handle was allocated automatically by mysql_init() or mysql_connect().

Return Values

None.

Errors

None.

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章