程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C >> 關於C >> c語言連接mysql原代碼實例

c語言連接mysql原代碼實例

編輯:關於C
不需要按照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[10]; 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); } }   if(strncmp("EC", strupr(father), strlen(father))==0) { father = strtok(NULL, "|#"); if (father != NULL) { printf("5"); index++; strcpy(log->ec, father); } }   if(strncmp("OAKEY", strupr(father), strlen(father))==0) { father = strtok(NULL, "|#"); if (father != NULL) { printf("6"); index++; strcpy(log->oaKey, father); } }   if(strncmp("TOUCHFW", strupr(father), strlen(father))==0) { father = strtok(NULL, "|#"); if (father != NULL) { printf("7"); index++; strcpy(log->touchFW, father); } }   if(strncmp("BATTERY", strupr(father), strlen(father))==0) { father = strtok(NULL, "|#"); if (father != NULL) { printf("8"); index++; log->batlvl = atoi(father); } }   father = strtok(NULL, "|#"); }   printf("\n%s, %s, %s, %s, %s, %s, %s, %d\n", log->sn, log->mac, log->biosV, log->biosD, log->ec, log->touchFW, log->oaKey, log->batlvl);   printf("index=%d", index); if (index != 8) return 1; else return 0; }   int main(int argc, char* argv[]) { FILE *f; DBINFO dbinfo; LOGS log; memset(&dbinfo, '\0', sizeof(dbinfo)); memset(&log, '\0', sizeof(log));   if (argc != 5) help();   if ((f=fopen(argv[4], "r"))==NULL) { fprintf(stderr, "\n] Error open file!\n"); return 1; }   if(parseLogs(f, &log)) { fprintf(stderr, "\n] Error log file format!\n"); return 1; }   strncpy(dbinfo.host, argv[1], strlen(argv[1])); strncpy(dbinfo.user, argv[2], strlen(argv[2])); strncpy(dbinfo.passwd, argv[3], strlen(argv[3])); strncpy(dbinfo.db, "test", strlen("test"));   return uplog(&dbinfo, &log);   }
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved