Chinaunix首页 | 论坛 | 博客
  • 博客访问: 233639
  • 博文数量: 113
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1044
  • 用 户 组: 普通用户
  • 注册时间: 2015-02-15 16:09
文章分类

全部博文(113)

文章存档

2016年(5)

2015年(108)

我的朋友

分类: C/C++

2015-08-30 01:57:51


点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <mysql.h>
  5. /* int main(int argc, char* argv[]) {
  6.     MYSQL mysql;
  7.     MYSQL_RES *rs=NULL;
  8.     MYSQL_ROW row;

  9.     if (connect_mysql(&mysql)) {
  10.         return 1;
  11.     }

  12.     if (getSection(&mysql)) {
  13.         mysql_close(&mysql);
  14.         return 1;
  15.     }

  16.     getChannelList(&mysql,&rs,1);
  17.     
  18.     while(row=mysql_fetch_row(rs))
  19.     {
  20.         printf("%d %d %d\n", atoi(row[0]),atoi(row[1]),atoi(row[2]));
  21.     }
  22.     
  23.      mysql_free_result(rs);    

  24.     mysql_close(&mysql);
  25.     return 0;
  26. }
  27.  */
  28.  /*connect    mysql*/
  29. int connect_mysql(MYSQL* mysql) {
  30.     printf("Initializing mysql.................");
  31.     if (!mysql_init(mysql)) {
  32.         return 1;
  33.     }
  34.     printf("Connectiong to mysql...............");
  35.     if (!mysql_real_connect(mysql, "localhost", "root", "1qazXSW2", "temperature4001", 0, NULL,0)) {
  36.         fprintf(stderr, "Error: %s\r\n", mysql_error(mysql));
  37.         return 1;
  38.     }
  39.     //设置mysql连接的字符集
  40.     mysql_query(mysql, "SET NAMES 'utf8'");    
  41.     return 0;
  42. }
  43. /*accept  DISTINCT  sectionID*/
  44. int getSection(MYSQL *mysql) {
  45.     int ret, field_count, row_count, i;
  46.     int *lengths;
  47.     char *query = "select DISTINCT sectionID from baseinfo";
  48.     MYSQL_RES *result;
  49.     MYSQL_ROW row;
  50.     ret = mysql_real_query(mysql, query, strlen(query));
  51.     if (ret != 0) {
  52.         printf("加载不了users的数据。\n");
  53.         return 1;
  54.     }
  55.     result = mysql_store_result(mysql);
  56.     row_count = (int) mysql_num_rows(result);
  57.     field_count = (int) mysql_num_fields(result);
  58. //    printf("the count is %d ,the field count %d\n",row_count,field_count);
  59.     if (result == NULL && field_count == 0) {
  60.         printf("无数据");
  61.         return 1;
  62.     }

  63.     while(row = mysql_fetch_row(result))
  64.     {
  65.          printf("%d \n", atoi(row[0]));
  66.     }

  67. //    for (i = 0; i < row_count; i++) {
  68. //        row = mysql_fetch_row(result);
  69. //        printf("%d \n", atoi(row[0]));
  70. //    }
  71.     mysql_free_result(result);
  72.     return 0;
  73. }
  74. /*获取函数*/
  75. int getChannelList(MYSQL *mysql,MYSQL_RES **result,int sectionID)
  76. {
  77.     char sql[500];

  78.     int ret;
  79.     
  80.         memset(sql,0x00,500);

  81.     sprintf(sql,"select a.baseID,a.deviceID,a.channelID,b.param3,b.param2,b.param1,b.param0 from baseinfo a,baseParam b where a.baseID=b.baseID AND a.flag=1 AND a.sectionID=%d order by a.deviceID, a.channelID,a.baseID ;",sectionID);

  82. //    printf("[INFO]: SQL is %s\n",sql);
  83.     
  84.     ret = mysql_real_query(mysql,sql, strlen(sql));    
  85.     if (ret != 0) {
  86.                 printf("exec sql %s error!\n",sql);
  87.                 return -1;
  88.         }

  89.     *result = mysql_store_result(mysql);

  90.     return 0;

  91. }
  92. /*插入数据*/
  93. int insertTempData(MYSQL *mysql,int baseID,double temperature)
  94. {
  95.     char sql[500];
  96.     
  97.     int ret;
  98.     
  99.        memset(sql,0x00,500);
  100.     
  101.     sprintf(sql,"INSERT INTO tempData(baseID,temperature) VALUES(%d,%f);",baseID,temperature);
  102.     
  103.     printf("[INFO]: SQL is %s\n",sql);
  104.     
  105.     ret = mysql_real_query(mysql,sql, strlen(sql));    
  106.     if (ret != 0) {
  107.                 printf("exec sql %s error!\n",sql);
  108.                 return -1;
  109.         }

  110.     
  111.     return 0;
  112. }

阅读(1188) | 评论(0) | 转发(0) |
0

上一篇:mysql C LINUX

下一篇:python笔记2

给主人留下些什么吧!~~