Chinaunix首页 | 论坛 | 博客
  • 博客访问: 402580
  • 博文数量: 58
  • 博客积分: 1775
  • 博客等级: 上尉
  • 技术积分: 755
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-12 15:03
文章分类

全部博文(58)

文章存档

2012年(5)

2011年(43)

2010年(10)

分类: LINUX

2011-10-13 04:21:14

  1. /*
  2.  ============================================================================
  3.  Name : MysqlTest.c
  4.  Author : SMBRAVE
  5.  Version :
  6.  Copyright : Your copyright notice
  7.  Description : Hello World in C, Ansi-style
  8.  ============================================================================
  9.  */

  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <mysql/mysql.h>

  14. #define CONFIG_FILE "update.conf"
  15. typedef struct m_config{
  16.     int version;
  17.     char *download_url;
  18.     char *database_name;
  19. }MConfig;
  20. //读取参数
  21. int ReadConf(MConfig *mconfig)
  22. {
  23.     FILE * fp;
  24.     char buf[1024],*p;
  25.     int len;
  26.     //打开文件失败
  27.     if((fp = fopen(CONFIG_FILE,"r")) == NULL){
  28.         perror("Conf open fail");
  29.         return -1;
  30.     }

  31.     //fgets遇到换行或EOF会结束
  32.     while(fgets(buf,sizeof(buf),fp)!=NULL)
  33.     {
  34.         if((buf[0]=='#') || (buf[0]=='\n') )
  35.             continue;//继续下一循环
  36.         len=strlen(buf);
  37.         if(buf[len-1]=='\n') buf[len-1]='\0';
  38.         //寻找’=‘及数据
  39.         if(((p=strchr(buf,'=')) == NULL) || (p==buf) )//if not find =
  40.             continue;
  41.         *p='\0';
  42.         p++;
  43.         for(len=0; len < strlen(buf); len++)
  44.             buf[len]=tolower(buf[len]);
  45.         printf("[Parameter]%s:%s\n",buf,p);
  46. ////////////////////////////////读取参数/////////////////////////////////////////////////////////
  47.         //版本号 参数
  48.         if(!strcmp(buf,"version")) mconfig->version = atoi(p);
  49.         //下载程序的地址 参数
  50.         if(!strcmp(buf,"download_url")){
  51.             mconfig->download_url = (char *)malloc(strlen(p) + 1);
  52.             strcpy(mconfig->download_url,p);
  53.         }

  54.         //数据库名 参数
  55.         if(!strcmp(buf,"database_name")){
  56.             mconfig->database_name = (char *)malloc(strlen(p) + 1);
  57.             strcpy(mconfig->database_name,p);
  58.         }
  59. ////////////////////////////////////////////////////////////////////////////////////////////////
  60.     }
  61.     return 0;
  62. }
  63. //释放参数
  64. int FreeConf(MConfig *mconf)
  65. {
  66.     //释放下载地址
  67.     if(mconf->download_url != NULL)
  68.         free(mconf->download_url);

  69.     //释放数据库名
  70.     if(mconf->database_name != NULL)
  71.         free(mconf->database_name);
  72. }

  73. int main(void)
  74. {
  75.     char *server = "localhost";
  76.     char *user = "ocard";
  77.     char *password = "ocardmysql";
  78.     MYSQL *mysql;
  79.     MYSQL_RES *res;
  80.     MYSQL_ROW row;
  81.     MConfig conf;

  82.     if(ReadConf(&conf)!=0)return -1;

  83.     mysql = mysql_init(NULL);
  84.     //连接数据库
  85.     if (!mysql_real_connect(mysql, server, user, password, conf.database_name, 0, NULL, 0)) {
  86.         fprintf(stderr, "%s\n", mysql_error(mysql));
  87.         return -1;
  88.     }

  89.     //更新
  90.     char update_sql[200];
  91.     sprintf(update_sql,"UPDATE SYSTEM_CONFIG SET "
  92.             "SYSTEM_LTIME=NOW(),SYSTEM_IVALUE=%d,SYSTEM_SVALUE='%s' "
  93.             "WHERE SYSTEM_KEY='POS_APP_UPDATE_INFO'",conf.version,conf.download_url);
  94.     if (mysql_real_query(mysql, update_sql,(unsigned int ) strlen(update_sql))) {
  95.         fprintf(stderr, "%s\n", mysql_error(mysql));
  96.         return -1;
  97.     }

  98.     //查询
  99.     char *select_sql = "SELECT * FROM SYSTEM_CONFIG WHERE SYSTEM_KEY='POS_APP_UPDATE_INFO'";
  100.     if (mysql_real_query(mysql, select_sql,(unsigned int ) strlen(select_sql))) {
  101.         fprintf(stderr, "%s\n", mysql_error(mysql));
  102.         return -1;
  103.     }
  104.     res = mysql_use_result(mysql);

  105.     while ((row = mysql_fetch_row(res)) != NULL){
  106.         int t;
  107.         for(t = 0;t < mysql_num_fields(res);t++)
  108.             printf("%s ",row[t]);
  109.         printf("\n");
  110.     }

  111.     FreeConf(&conf);
  112.     mysql_free_result(res);
  113.     mysql_close(mysql);
  114.     return EXIT_SUCCESS;
  115. }
阅读(1902) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~