分类: Mysql/postgreSQL
2010-01-04 20:27:47
c) Issue SQL statements and process their results
The client can send SQL statement to the server using mysql_query() or mysql_real_query() The diff is that mysql_query() expects the query to be specified as a null-terminated string whereas mysql_real_query() expects a counted string. If the string contains binary data(which may include null bytes), you must use mysql_real_query().
For SELECT queries, mysql_store_result() get all the row returned from the server and stores them in the client. And mysql_use_result() initiates a row-by-row result set, but does not actually get any rows from the server. In both cases, you access rows by calling mysql_fetch_row().
After you are done with a result set, call mysql_free_result() to free the memory used for it.
if using mysql_store_result, you can move back and forth in the result set by using mysql_data_seek() or mysql_row_seek(), and you can also find out how many rows there are by calling mysql_num_rows().
id
|
func
|
Description
|
1
|
mysql_library_init()
|
初始化mysql library. 对于mysql单线程场景,可以忽略,mysql_init()默认会调用这个函数。对于多线程场景,应当在起始时调用。 |
2
|
mysql_library_end()
|
对应mysql_library_end(),建议调用。
|
3
|
mysql_init()
|
初始化MYSQL结构,
|
4
|
mysql_real_connect()
|
给定host, user, passwd, port,dbname等,连接mysql server。注意不应当再使用过期的mysql_connect() |
5
|
mysql_close()
|
关闭连接。
|
6
|
mysql_store_result()
|
查询结果集合全部取回client
|
7
|
mysql_use_result()
|
一行一行的取回查询结果集合
|
8
|
mysql_free_result()
|
释放结果集合占用的内存
|
9
|
mysql_fetch_row()
|
取结果集合中的下一行
|
10
|
mysql_errno()
|
错误码
|
11
|
mysql_error()
|
错误信息
|
mysql> desc city;
| ID | int(11) | NO | PRI | NULL | auto_increment |
| Name | char(35) | NO | | | |
| CountryCode | char(3) | NO | | | |
| District | char(20) | NO | | | |
| Population | int(11) | NO | | 0 | |
5 rows in set (0.00 sec)
unsigned int r;
printf("1. inited ---------------\n");
if (!mysql_real_connect(&mySQLHandler, MYSQL_HOST, MYSQL_USER, MYSQL_PASSWD, MYSQL_DB,MYSQL_PORT, NULL, 0))
return 1;
printf("2. connected ---------------\n");
r = mysql_real_query(&mySQLHandler, MYSQL_SELECTSTATEMENT, (unsigned long) strlen(MYSQL_SELECTSTATEMENT));
return 1;
printf("3.queried ---------------\n");
if (!mySQLRes)
return 1;
printf("3.stored result ---------------\n");
while(mySQLRow = mysql_fetch_row(mySQLRes))
printf("4.fetched rows ---------------\n");
printf("5.free result ---------------\n");
printf("6.close mysql ---------------\n");
return 0;