Chinaunix首页 | 论坛 | 博客
  • 博客访问: 199217
  • 博文数量: 69
  • 博客积分: 1400
  • 博客等级: 上尉
  • 技术积分: 720
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-03 11:35
文章分类

全部博文(69)

文章存档

2011年(13)

2010年(46)

2009年(10)

我的朋友

分类: Mysql/postgreSQL

2010-01-04 20:27:47

MySql 100104:Connector/C

1.框架

基本步骤:
a) mysql_library_init()
b)mysql_init()

c) Issue SQL statements and process their results

d) mysql_close()
e) mysql_library_end()
 

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

 

2.主要函数

 
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()
错误信息
 

3.例子

预备条件:
server上的账户 root:passwd@localhost:3306, 数据库是world, 其中的表city结构如下
 

mysql> desc city;

+-------------+----------+------+-----+---------+----------------+
| Field       | Type     | Null | Key | Default | Extra          |
+-------------+----------+------+-----+---------+----------------+

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

 
 
程序如下
 
// mysqlTest.cpp : 定义控制台应用程序的入口点。
//
 
#include "stdafx.h"
#include "mysql.h"
 
 
#define MYSQL_PORT 3306
#define MYSQL_HOST "localhost"
#define MYSQL_USER "root"
#define MYSQL_PASSWD   "passwd"
#define MYSQL_DB   "world"
 
#define MYSQL_SELECTSTATEMENT    "select id, name, countrycode from city"
 
 
MYSQL mySQLHandler;
 
int _tmain(int argc, _TCHAR* argv[])
{
 

     unsigned int r;

     MYSQL_RES *mySQLRes;
     MYSQL_ROW mySQLRow;
    
     mysql_library_init(0,NULL,NULL);
 
     mysql_init(&mySQLHandler);
 

     printf("1. inited ---------------\n");

 

     if (!mysql_real_connect(&mySQLHandler, MYSQL_HOST, MYSQL_USER, MYSQL_PASSWD, MYSQL_DB,MYSQL_PORT, NULL, 0))

     {
         printf( "Error connecting to database: %s\n",mysql_error(&mySQLHandler));

         return 1;

     }
 

     printf("2. connected ---------------\n");

 

     r = mysql_real_query(&mySQLHandler, MYSQL_SELECTSTATEMENT, (unsigned long) strlen(MYSQL_SELECTSTATEMENT));

 
     if(r)   
     {
         printf( "query err: %s\n",mysql_error(&mySQLHandler));

         return 1;

     }
 

     printf("3.queried ---------------\n");

 
     mySQLRes = mysql_store_result(&mySQLHandler);
 

     if (!mySQLRes)    

     {
         printf( "store result err: %s\n",mysql_error(&mySQLHandler));

         return 1;

     }
 

     printf("3.stored result ---------------\n");

 

     while(mySQLRow = mysql_fetch_row(mySQLRes))

     {
         for(r=0;r
         {
              printf("%s ",mySQLRow[r]);
         }
         printf("\n");
     }
 

     printf("4.fetched rows ---------------\n");

     mysql_free_result(mySQLRes);
 

     printf("5.free result ---------------\n");

 
     mysql_close(&mySQLHandler);

     printf("6.close mysql ---------------\n");

 
//   mysql_library_end();
 

     return 0;

}
 
阅读(1331) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~