首先你的安裝一個linux系統
- 建議安裝Ubnutu,因爲這個系統比較全比Centos感覺要好些,而且圖形界面也可以的,最關鍵的是Ubnutu庫中有mysql.h,而centos中是沒有的,網上有Ubnutu的安裝方法,這裏就不過多講解了。
在安裝一個ssh的遠程客戶端,關閉linux的防火牆
1) 重啓後生效
開啓: chkconfig iptables on
關閉: chkconfig iptables off2) 即時生效,重啓後失效
開啓: service iptables start
關閉: service iptables stop
3)安裝ssh在linux上的連接端以便windows下的ssh連接- 安裝mysql
首先先檢查是否安裝mysql
sudo netstat -tap | grep mysql
如果沒有就安裝mysql
sudo apt-get install mysql-server mysql-client
並授權 grant all privileges on . to ‘root’@’%’ identified by ‘xxxxxx’;
在linux系統下開放mysql 的端口號
你需要導入txt文件和.c文件需要放在/var/lib/mysql-files/目錄下才能行,因爲mysql有本身設置txt文件讀寫權限控制的,還有一些相關配置這裏就不詳解了。
創建庫並創建表 - 代碼如下
將代碼拷貝到/var/lib/mysql-files下
運行命令
sudo gedit connect.c
運行命令
gcc -I/usr/include/mysql/ connect.c -L/usr/lib/mysql -lmyclient -o connect
運行編譯後的connect
./connect
//connect.c
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "mysql.h"
//Ubuntu下要安裝mysql的一些相關的客戶端才能導入mysql.h
int main(int argc, char *argv[]) {
struct timeval start, end;
time_t t;
struct tm * lt;
MYSQL my_connection;
int res;
char sql[200];
char starttime[200];
char sta[200];
char endtime[200];
mysql_init(&my_connection);
if (mysql_real_connect(&my_connection, "localhost",
"root", "root", "foo", 0, NULL, 0)) {
printf("Connection success\n");
gettimeofday( &start, NULL );
//starttime
time (&t);//獲取Unix時間戳。
lt = localtime (&t);//轉爲時間結構。
sprintf ( starttime,"%d/%d/%d %d:%d:%d\n",lt->tm_year+1900, lt->tm_mon+1, lt->tm_mday, lt->tm_hour, lt->tm_min, lt->tm_sec);//輸出結果
//insert startime
sprintf(sta,"%s",starttime);
sprintf(sql,"insert into log(starttime,sucessnum,failnum,endtime) values('%s',0,0,'')",sta);
printf(sql);//寫入日誌表,你可以把日誌表刪了,這個對你沒影響的
res = mysql_query(&my_connection, sql );
res = mysql_query(&my_connection, "load data local infile '2.txt' into table zhucexinxi6 fields terminated by' ' (bianhao,dabianhao,jixian,dengji ,biaoshi ,zhucedatetime,username ,tel ,weizhi ,mark ,successtime ,a ,b ,leavetime ,state)" );//sql導入txt文件
//endtime
time (&t);//獲取Unix時間戳。
lt = localtime (&t);//轉爲時間結構。
printf ( "%d/%d/%d %d:%d:%d\n",lt->tm_year+1900, lt->tm_mon, lt->tm_mday, lt->tm_hour, lt->tm_min, lt->tm_sec);//輸出結果
sprintf ( endtime,"%d/%d/%d %d:%d:%d\n",lt->tm_year+1900, lt->tm_mon+1, lt->tm_mday, lt->tm_hour, lt->tm_min, lt->tm_sec);//輸出結果
//update endtime
sprintf(sql,"update log set sucessnum =%d,failnum=%d,endtime='%s' where starttime='%s'",
(unsigned long)mysql_affected_rows(&my_connection),
mysql_errno(&my_connection),//修改日誌表
endtime,
sta);
printf(sql);
res = mysql_query(&my_connection, sql);//執行sql
//獲得當前時間,精確到毫秒
gettimeofday( &end, NULL );
int timeuse = 1000000 * ( end.tv_sec - start.tv_sec ) + end.tv_usec - start.tv_usec;
printf("time: %d us\n", timeuse);
if (!res) {
printf("Inserted %lu rows\n",
(unsigned long)mysql_affected_rows(&my_connection));
} else {
fprintf(stderr, "Insert error %d: %s\n", mysql_errno(&my_connection), mysql_error(&my_connection));
}
//關閉sql連接
mysql_close(&my_connection);
} else {
fprintf(stderr, "Connection failed\n");
if (mysql_error(&my_connection)) {
fprintf(stderr, "Connection error %d: %s\n", mysql_errno(&my_connection), mysql_error(&my_connection));
}
}
return EXIT_SUCCESS;
}
- txt文件如下
998 157 11021543
998 159 11031545
998 151 11041541
是以這樣的順序排列的 - sql導入代碼詳解
load data local infile '2.txt' into table zhucexinxi6//加載txt文件
fields terminated by' ' //字段分割取值
(bianhao,dabianhao,jixian,dengji ,biaoshi ,zhucedatetime,username ,tel ,weizhi ,mark ,successtime ,a ,b ,leavetime ,state)//插入對應表中的列值中
基本上到了這裏就結束了, 如果覺得可以就請收藏轉載