兴海北路

---男儿仗剑自横行
<2008年3月>
2425262728291
2345678
9101112131415
16171819202122
23242526272829
303112345

统计

  • 随笔 - 85
  • 文章 - 0
  • 评论 - 17
  • 引用 - 0

常用链接

留言簿(6)

随笔分类

随笔档案

收藏夹

全是知识啊

搜索

  •  

最新评论

阅读排行榜

评论排行榜

VC使用C API 连接操作MySQL数据库

一切尽在代码中,代码中也太多了if/else,可以对它进行更好的函数及至类的封装,规范的处理好异常。

  1. #include <windows.h>
  2. //需要在VC的Options设置一个include路径指向'%mysql_home%/inlude'目录   
  3. #include <mysql.h>
  4. //设置一个lib路径指向'%mysql_home%/lib/opt'目录 (mysql5.0是个目录)   
  5. #pragma comment(lib,"libmysql.lib")    
  6. #define host_name "localhost"  //数据库服务器   
  7. #define db_name "test"         //数据库名   
  8. #define user_name "root"       //用户名   
  9. #define password ""            //密码   
  10. int  main( int  argc, char  * argv[]) {   
  11.     
  12.   char  szSqlText[500] ;   
  13.     
  14.  MYSQL *conn;   
  15.  MYSQL_RES *rs;   
  16.  MYSQL_ROW row;    //注意它的声明 typedef char **MYSQL_ROW,字符串数组   
  17.   BOOL  bCreate = FALSE;   
  18.     
  19.  conn = mysql_init(NULL);   
  20.   if (conn == NULL)   
  21.  {   
  22.   fprintf(stderr, "mysql_init() failed (probably out of memory)\n" );   
  23.   exit(1);   
  24.  }   
  25.     
  26.   if  (mysql_real_connect(conn,host_name,user_name,password,   
  27.   db_name,MYSQL_PORT,NULL,0) == NULL)   
  28.  {   
  29.    //在MYSQL初始化之后的操作如果有错误,可以用mysql_errno(MYSQL*)和   
  30.    //mysql_errer(MYSQL*) 分别获得出错代号和描述   
  31.   fprintf(stderr, "mysql_real_connect() failed:\nError %u (%s)\n" ,   
  32.    mysql_errno(conn),mysql_error(conn));   
  33.   exit(1);   
  34.  }   
  35.     
  36.  printf( "connect to db successful.\n" );   
  37.   if  (bCreate) {   
  38.    //第一次运行创建一个表mytable   
  39.   sprintf(szSqlText, "create table mytable(time datetime,s1 char(6),s2 char(11),s3 int,s4 int)" );   
  40.    if  (mysql_query(conn,szSqlText)) {   
  41.    printf( "Can't create table.\n" );   
  42.    mysql_close(conn);   
  43.     return  0;   
  44.   }   
  45.  }   
  46.  sprintf(szSqlText, "insert into mytable values('2000-3-10 21:01:30','Test','MySQLTest',2000,3)" );   
  47.   if  (mysql_query(conn,szSqlText)) {   
  48.   printf( "Insert values error:\nError %u (%s)\n" ,   
  49.    mysql_errno(conn),mysql_error(conn));   
  50.   mysql_close(conn);   
  51.    return  0;   
  52.  }   
  53.   else {   
  54.    //insert/delete/update 语句可用mysql-affected_rows()得到受作用的行   
  55.   printf( "INSERT statement succeeded: %lu rows affected\n" ,   
  56.    (unsigned  long )mysql_affected_rows(conn));   
  57.  }   
  58.     
  59.   //查询数据   
  60.  sprintf(szSqlText, "select * from mytable" );   
  61.     
  62.   //执行成功则返回零   
  63.   if  (mysql_query(conn,szSqlText) != 0) {   
  64.   mysql_close(conn);   
  65.    return  0;   
  66.  }   
  67.   else  {   
  68.    //立即从服务器返回所有行,存储到本地,产生结果集,失败则返回NULL   
  69.   rs = mysql_store_result(conn);   
  70.      
  71.    //结果集是保留在服务器上,fetch_row时才逐行从服务器上取   
  72.    //rs = mysql_use_result(conn);   
  73.    //Get query result.   
  74.    int  count = ( int )mysql_num_rows(rs);   
  75.   printf( "Query: %s.\n%ld records found.\n" ,szSqlText,count);   
  76.    //MYSQL_ROW是一个指向数值数组的指针,row[0],row[1]...row[列数-1]   
  77.    //所有的数据类型都以字符串返回,即使是数字型,要进行串转换   
  78.    //NULL指针代表数据库字段的NULL,应经常检查列是否为NULL   
  79.    while ((row = mysql_fetch_row(rs)) != NULL){   //返回NULL,则说明不再有行   
  80.     for (unsigned  int  i=0; i<mysql_num_fields(rs);i++){   
  81.      if (i>0)   
  82.     fputc('\t',stdout);   
  83.     printf( "%s" ,row[i]!=NULL ? row[i]: "NULL" );   
  84.    }   
  85.    fputc('\n',stdout);   
  86.   }   
  87.    //使用完后,释放结果集占用内存   
  88.   mysql_free_result(rs);   
  89.  }   
  90.  mysql_close(conn);   
  91.   return  0;   
  92. }   
  93. //最后,注意查询语句中字符串的转义 select a from t where a=''1',是要出错的   
  94. //空字符转换为'\0',这里的0 是可打印的ASCII 码0,而不是空。   
  95. //反斜线、单引号和双引号分别转换为‘\\’、‘\'’ 和‘\"’   
  96. //你也可以用mysql_escape_string(to_str,from_str,from_len)转换sql语句   
输出结果是:
connect to db successful.
INSERT statement succeeded: 1 rows affected
Query: select * from mytable.
8 records found.
2000-03-10 21:01:30     Test    MySQLTest       2000    3
2000-03-10 21:01:30     Test    MySQLTest       2000    3
2000-03-10 21:01:30     Test    MySQLTest       2000    3
2000-03-10 21:01:30     Test    MySQLTest       2000    3
2000-03-10 21:01:30     Test    MySQLTest       2000    3
Press any key to continue

posted on 2008-04-08 14:50 随意门 阅读(621) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理