免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2655 | 回复: 0
打印 上一主题 下一主题

MySql 100104:Connector/C [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-01-04 20:27 |只看该作者 |倒序浏览

               

  Normal
  0
  
  7.8 磅
  0
  2
  
  false
  false
  false
  
   
   
   
   
   
   
   
   
   
   
   
   
  
  MicrosoftInternetExplorer4



/* Style Definitions */
table.MsoNormalTable
        {mso-style-name:普通表格;
        mso-tstyle-rowband-size:0;
        mso-tstyle-colband-size:0;
        mso-style-noshow:yes;
        mso-style-parent:"";
        mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
        mso-para-margin:0cm;
        mso-para-margin-bottom:.0001pt;
        mso-pagination:widow-orphan;
        font-size:10.0pt;
        font-family:"Times New Roman";
        mso-ansi-language:#0400;
        mso-fareast-language:#0400;
        mso-bidi-language:#0400;}
table.MsoTableGrid
        {mso-style-name:网格型;
        mso-tstyle-rowband-size:0;
        mso-tstyle-colband-size:0;
        border:solid windowtext 1.0pt;
        mso-border-alt:solid windowtext .5pt;
        mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
        mso-border-insideh:.5pt solid windowtext;
        mso-border-insidev:.5pt solid windowtext;
        mso-para-margin:0cm;
        mso-para-margin-bottom:.0001pt;
        text-align:justify;
        text-justify:inter-ideograph;
        mso-pagination:none;
        font-size:10.0pt;
        font-family:"Times New Roman";
        mso-ansi-language:#0400;
        mso-fareast-language:#0400;
        mso-bidi-language:#0400;}
MySql 100104:Connector/C
@
http://zcatt.cublog.cn

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;
}

               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/8866/showart_2139333.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP