linux下如何使用c导入txt文件进行mysql数据库操作

首先你的安装一个linux系统

  1. 建议安装Ubnutu,因为这个系统比较全比Centos感觉要好些,而且图形界面也可以的,最关键的是Ubnutu库中有mysql.h,而centos中是没有的,网上有Ubnutu的安装方法,这里就不过多讲解了。
  2. 在安装一个ssh的远程客户端,关闭linux的防火墙
    1) 重启后生效
    开启: chkconfig iptables on
    关闭: chkconfig iptables off

    2) 即时生效,重启后失效
    开启: service iptables start
    关闭: service iptables stop
    3)安装ssh在linux上的连接端以便windows下的ssh连接

  3. 安装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文件读写权限控制的,还有一些相关配置这里就不详解了。
    创建库并创建表
  4. 代码如下
    将代码拷贝到/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;
}
  1. txt文件如下
    998 157 11021543
    998 159 11031545
    998 151 11041541
    是以这样的顺序排列的
  2. 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)//插入对应表中的列值中

基本上到了这里就结束了, 如果觉得可以就请收藏转载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值