设为首页 加入收藏

TOP

c语言连接mysql原代码实例(一)
2013-12-05 12:46:49 来源: 作者: 【 】 浏览:843
Tags:语言 连接 mysql 代码 实例

  不需要按照ODBC驱动。通过mysql自带的3306端口进行数据传输。

  注意libmysq.dll与对应服务器版本的关系。

  // uplog.cpp : Defines the entry point for the console application.

  //

  #include "stdafx.h"

  #include <stdio.h>

  #include <stdlib.h>

  #include <string.h>

  #include <mysql/mysql.h>

  #pragma comment(lib, "libmysql.lib")

  struct DBINFO

  {

  char host[20];

  char user[20];

  char passwd[20];

  char db[20];

  };

  struct LOGS{

  char sn[20];

  char mac[20];

  char biosV[30];

  char biosD[30];

  char ec ;

  char touchFW[20];

  char oaKey[35];

  int  batlvl;

  };

  void help(void)

  {

  fprintf(stderr, "uplog <host> <user> <pwd> <logfile>");

  exit(1);

  }

  int uplog(DBINFO *info, LOGS *logs)

  {

  MYSQL *my = NULL;

  char sql[512];

  printf("\nUpload test logs to DB server…\n");

  my = mysql_init(NULL);

  if (my == NULL)

  {

  fprintf(stderr, "\n] Error: Unable to init APIs…\n");

  return 1;

  }

  if(mysql_real_connect(my, info->host, info->user, info->passwd, NULL, MYSQL_PORT, NULL, 0) == 0)

  {

  fprintf(stderr, "\n] Error: Unable to connect server…\n");

  if(my!=NULL)

  mysql_close(my);

  return 1;

  }

  if (mysql_select_db(my, info->db)<0)

  {

  fprintf(stderr, "\n] Error: Unable to select db(%s)…\n", info->db);

  if(my!=NULL)

  mysql_close(my);

  return 1;

  }

  sprintf(sql, "INSERT INTO `testlogs` (`Serial_number`, `Mac_addr`, `BIOS_version`, `BIOS_date`, `EC_version`, `Touch_FW`, `OA_KEY`, `Battery_level`) VALUES ("

  "'%s', '%s', '%s', '%s', '%s', '%s', '%s', %d)",logs->sn, logs->mac, logs->biosV, logs->biosD, logs->ec, logs->touchFW, logs->oaKey, logs->batlvl);

  if(mysql_query(my, sql) != 0)

  {

  printf("\n] Error: Unable to execute query…\n");

  if (my!=NULL)

  mysql_close(my);

  return 1;

  }

  mysql_close(my);

  printf("%s\n Upload to DB ok\n", sql);

  return 0;

  }

  int parseLogs(FILE *f, LOGS *log)

  {

  char buf[512];

  char *father;

  int index = 0;

  printf("parsing logs… ");

  fgets(buf, 512, f);

  if(strlen(buf) < 10) return 1;

  /**

  parsing log format, like |KEY1#VALUE1|KEY2#VALUE2

  */

  father = strtok(buf, "|#");

  while(father != NULL)

  {

  if(strncmp("MACADDR", strupr(father), strlen(father))==0)

  {

  father = strtok(NULL, "|#");

  if (father != NULL)

  {

  printf("1");

  index++;

  strcpy(log->mac, father);

  }

  }

  if(strncmp("SN", strupr(father), strlen(father))==0)

  {

  father = strtok(NULL, "|#");

  if (father != NULL)

  {

  printf("2");

  index++;

  strcpy(log->sn, father);

  }

  }

  if(strncmp("BIOSD", strupr(father), strlen(father))==0)

  {

  father = strtok(NULL, "|#");

  if (father != NULL)

  {

  printf("3");

  index++;

  strcpy(log->biosD, father);

  }

  }

  if(strncmp("BIOSV", strupr(father), strlen(father))==0)

  {

  father = strtok(NULL, "|#");

  if (father != NULL)

  {

  printf("4");

  index++;

  strcpy(log->biosV, father);

  }

  }

   

首页 上一页 1 2 3 4 5 6 7 下一页 尾页 1/8/8
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C语言的struct的数据成员对齐 下一篇二叉查找树

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: