废话不多说, 直接上代码,我也是根据refman 文档编写的,在网上找了一圈,没找到相应的访问代码。

第一步: 首先要安装MYSQL相关开发包,主要安装如下两个包就能访问MySQL库了,linux下安装比较简单,直接执行如下命令即可:

rpm  -ivh  mysql-libs-5.1.61-4.el6.x86_64
rpm  -ivh  mysql-devel-5.1.61-4.el6.x86_64

第二步:编写访问mysql的C代码, 假设要访问 192.169.33.145 机器上的test 数据库的ttt表, 用户名 , scott, tiger , C代码文件名称为 mqtst.c

  1. #include<stdio.h>
  2. #include<mysql/mysql.h>
  3. #include<string.h>
  4. #define LEN_NAME 30
  5. int main(int argc ,char** argv)
  6. {
  7.     MYSQL* mysql ;
  8.     MYSQL_STMT* stmt;
  9.     MYSQL_BIND bind[2];
  10.     int i = 0 ;
  11.     char sname[3][LEN_NAME+1]={"zhangsan","lisi","wangwu"};
  12.     int tid[3] = {0,1,2} ;
  13.     unsigned long ulname ;
  14.     char rstmt[] = "insert into ttt(c0,c1)values(?,?)";

  15.     mysql = mysql_init(NULL);
  16.     if(mysql == NULL)
  17.     {
  18.         return -1 ;
  19.     }
  20.     if(! mysql_real_connect(mysql,"192.169.33.145","scott","tiger","test",0,0,0))
  21.     {
  22.         printf("connect database error [%d,%s]\n",mysql_errno(mysql),mysql_error(mysql));
  23.         mysql_close(mysql);
  24.         return -1;
  25.     }
  26.     //
  27.     stmt = mysql_stmt_init(mysql);
  28.     if(!stmt)
  29.     {
  30.         printf("init stmt error %s\n",mysql_error(mysql));
  31.         mysql_close(mysql);
  32.         return -1 ;
  33.     }
  34.     if( mysql_stmt_prepare(stmt,rstmt,strlen(rstmt)))
  35.     {
  36.         printf("mysql stmt prepare error (%d,%s)\n",mysql_errno(mysql),mysql_error(mysql));
  37.         mysql_stmt_close(stmt);
  38.         mysql_close(mysql);
  39.         return -1 ;
  40.     }
  41.     memset(bind,0,sizeof(bind));
  42.     for(i = 0 ; i < 3; ++i)
  43.     {
  44.         bind[0].buffer_type = MYSQL_TYPE_LONG ;
  45.         bind[0].buffer = (char*)& tid[i];
  46.         bind[0].is_null = 0 ;            
  47.         
  48.         bind[1].buffer_type = MYSQL_TYPE_STRING ;
  49.         bind[1].buffer = (char*)sname[i];
  50.         bind[1].buffer_length = LEN_NAME ;
  51.         bind[1].length = &ulname;
  52.         bind[1].is_null = 0 ;
  53.         ulname = strlen(sname[i]);

  54.         if(mysql_stmt_bind_param(stmt,bind))    
  55.         {
  56.             printf("mysql_stmt_bind_param error (%d,%s)\n",mysql_stmt_errno(stmt),mysql_stmt_error(stmt));
  57.             mysql_stmt_close(stmt);
  58.             mysql_close(mysql);
  59.             return -1 ;
  60.         }
  61.         if(mysql_stmt_execute(stmt))
  62.         {
  63.             printf("mysql_stmt_execute error: %s\n",mysql_stmt_error(stmt));
  64.             mysql_stmt_close(stmt);
  65.             mysql_close(mysql);
  66.             return -1 ;
  67.         }
  68.         printf("affect rows = %ld \n",mysql_affected_rows(mysql));
  69.     }
  70.     mysql_stmt_close(stmt);
  71.     mysql_close(mysql);
  72.     return 0 ;
  73. }
第三步,编译代码,前提机器一定要安装编译器cc ,执行如下命令
-bash-4.1$ cc -o mqtst mqtst.c  -lmysqlclient 
-bash-4.1$ 

第四步,执行./mqtst 

第五步,检查是否已经插入数据

mysql> select * from ttt ;
+----+----------+
| c0 | c1       |
+----+----------+
|  0 | zhangsan |
|  1 | lisi     |
|  2 | wangwu   |
+----+----------+
3 rows in set


mysql> 





09-05 17:36