c语言访问mysql 完整例子

来源:互联网 发布:格罗兹尼巷战 知乎 编辑:程序博客网 时间:2024/05/22 08:26
[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:18px;">1、手写安装带mysql sdk 的mysql  
  2. 2、新建控制台项目,项目属性中把  
  3. C:\Program Files\MySQL\MySQL Server 5.5\include 加入“包含目录”  
  4. 把C:\Program Files\MySQL\MySQL Server 5.5\lib 加入“库目录”  
  5.   
  6. 3、stdafx.h中加入  
  7. #include <winsock.h> //注意顺序,要放在mysql.h前  
  8. #include <mysql.h>//控制台项目中要在mysql.h之前include <winsock.h>  
  9.   
  10. //注意lib添加到“库目录”中,而不是“引用目录”中  
  11. #pragma comment(lib, "libmysql.lib")  
  12.   
  13. 4、把libmysql.dll放到生成的exe目录下  
  14.   
  15. 5、主要的几个例子:  
  16. //执行基本查询  
  17. void test1()  
  18. {  
  19.  MYSQL *pConn;  
  20.  pConn = mysql_init(NULL);  
  21.  //第2、3、4、5参数的意思分别是:服务器地址、用户名、密码、数据库名,第6个为mysql端口号(0为默认值3306)  
  22.  if(!mysql_real_connect(pConn,"localhost","root","root","test",0,NULL,0))  
  23.  {    
  24.   printf("无法连接数据库:%s",mysql_error(pConn));  
  25.   return;  
  26.  }  
  27.  mysql_query(pConn,"set names gbk");//防止乱码。设置和数据库的编码一致就不会乱码  
  28.  //SET NAMES x 相当于 SET character_set_client = x;SET character_set_results = x;SET character_set_connection = x;  
  29.  //写set character set gbk;查询不会乱码,但是参数化插入会报错。而set names gbk则都不会乱码  
  30.   
  31.   
  32.  //mysql_real_query比mysql_query多了个参数: 字符串query的长度, 所以适合有二进制数据的query, 而mysql_query的字符串query不能包含二进制,因为它以\0为结尾  
  33.  //mysql_query() 不能传二进制BLOB字段,因为二进制信息中的\0会被误判为语句结束。 mysql_real_query() 则可以。  
  34.  if(mysql_query(pConn,"select * from persons"))  
  35.  {  
  36.   printf("查询失败:%s",mysql_error(pConn));  
  37.   return;  
  38.  }  
  39.   
  40.  //mysql_store_result是把查询结果一次性取到客户端的离线数据集,当结果比较大时耗内存。  
  41.  //mysql_use_result则是查询结果放在服务器上,客户端通过指针逐行读取,节省客户端内存。但是一个MYSQL*连接同时只能有一个未关闭的mysql_use_result查询  
  42.  MYSQL_RES *result = mysql_store_result(pConn);  
  43.  MYSQL_ROW row;  
  44.  while(row = mysql_fetch_row(result))  
  45.  {  
  46.   printf("%s %s\n",row[1],row[2]);  
  47.  }  
  48.   
  49.  mysql_free_result(result);  
  50.  mysql_close(pConn);  
  51. }  
  52.   
  53. //获得更新行数  
  54. void test2()  
  55. {  
  56.  MYSQL *pConn;  
  57.  pConn = mysql_init(NULL);  
  58.  if(!mysql_real_connect(pConn,"127.0.0.1","root","root","test",0,NULL,0))  
  59.  {  
  60.   printf("无法连接数据库:%s",mysql_error(pConn));  
  61.   return;  
  62.  }  
  63.  if(mysql_query(pConn,"update persons set Age=Age+1"))  
  64.  {  
  65.   printf("执行失败:%s",mysql_error(pConn));  
  66.   return;  
  67.  }  
  68.  printf("更新成功,共更新完成%d条",mysql_affected_rows(pConn));  
  69.  mysql_close(pConn);  
  70. }  
  71.   
  72. //获得自增id  
  73. void test3()  
  74. {  
  75.  MYSQL *pConn;  
  76.  pConn = mysql_init(NULL);  
  77.  if(!mysql_real_connect(pConn,"127.0.0.1","root","root","test",0,NULL,0))  
  78.  {  
  79.   printf("无法连接数据库:%s",mysql_error(pConn));  
  80.   return;  
  81.  }  
  82.  mysql_query(pConn,"set names gbk");  
  83.  if(mysql_query(pConn,"insert into persons(Name,Age) values('传智播客',100)"))  
  84.  {  
  85.   printf("执行insert失败%s",mysql_error(pConn));  
  86.   return;  
  87.  }  
  88.  printf("执行insert成功,新id=%d",mysql_insert_id(pConn));  
  89.  mysql_close(pConn);  
  90.   
  91. }  
  92.   
  93. //参数化查询  
  94. void test4()  
  95. {  
  96.  MYSQL* pConn;  
  97.  pConn = mysql_init(NULL);  
  98.  if(!mysql_real_connect(pConn,"127.0.0.1","root","root","test",0,NULL,0))  
  99.  {  
  100.   printf("数据库连接失败:%s",mysql_error(pConn));  
  101.   return;  
  102.  }  
  103.   
  104.  mysql_query(pConn,"set names gbk");  
  105.  MYSQL_STMT    *stmt;      
  106.     MYSQL_BIND    bind[2];     
  107.  memset(bind,0,sizeof(bind));//把is_null、length等字段默认值设置为NULL等默认值,否则执行会报错  
  108.   
  109.  stmt = mysql_stmt_init(pConn);    
  110.  char* insertSQL="insert into persons(Name,Age) values(?,?)";  
  111.     if (mysql_stmt_prepare(stmt, insertSQL, strlen(insertSQL)))      
  112.     {      
  113.         fprintf(stderr, " mysql_stmt_prepare(), INSERT failed,%s\n",mysql_error(pConn));   
  114.         return;      
  115.     }      
  116.  bind[0].buffer_type= MYSQL_TYPE_STRING;      
  117.     bind[0].buffer= "黑马";      
  118.     bind[0].buffer_length= strlen("黑马"); //如果设定了buffer_length,则可以不试用length  
  119.       
  120.     int age=3;  
  121.   
  122.  bind[1].buffer_type= MYSQL_TYPE_LONG;      
  123.     bind[1].buffer= &age;    
  124.  bind[1].buffer_length = sizeof(age);  
  125.        
  126.     if (mysql_stmt_bind_param(stmt, bind))      
  127.     {      
  128.         fprintf(stderr, " mysql_stmt_bind_param() failed %s\n", mysql_stmt_error(stmt));      
  129.         return;  
  130.     }      
  131.       
  132.     if (mysql_stmt_execute(stmt))      
  133.     {      
  134.         fprintf(stderr, " mysql_stmt_execute(), failed %s\n", mysql_stmt_error(stmt));      
  135.         return;     
  136.     }      
  137.  mysql_stmt_close(stmt);  
  138.  mysql_close(pConn);    
  139.  printf("参数化执行SQL结束");  
  140. }</span>  
0 0
原创粉丝点击