码迷,mamicode.com
首页 > 数据库 > 详细

csv文件快速转存到mysql

时间:2019-12-10 22:46:25      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:读取   update   except   close   exce   提交   data   char   col   

csv文件快速转存到mysql

连接数据库

连接数据库:
    con = pymysql.connect(user="root",
                      passwd="root",
                      db="test",
                      host="47.95.xxx.xxx", 
                      local_infile=1)

将csv批量写到数据库,需要设置local_infile参数,如果不添加会报错。!

连接完数据库我们便可以使用游标来执行sql语句了:

cur = con.cursor()
定义好了游标我们就可以使用execute方法来执行sql语句了。

cur.execute("set names utf8")
cur.execute("SET character_set_connection=utf8;")

读取csv文件内容:

with open(file_path, 'r', encoding='utf8') as f:
    reader = f.readline()
    print(reader)
    devide = reader.split(',')  # 做成列表
    devide[-1] = devide[-1].rstrip('\n')   # 去除最后的换行符
    print(devide)

创表:

需要创建表,在创建表之前我们需要将每个列指定一下格式:

column = ''
for dd in devide:
    column = column + dd + ' varchar(255),'

拼接好后我们需要将最后一个列的逗号去掉
col = column.rstrip(',')

创建表:
table_name = "TBexport"
create_table_sql = 'create table if not exists {} ({}) DEFAULT CHARSET=utf8'    .format(table_name, col)
cur.execute(create_table_sql)


插入数据的语句:

file_path = "export.csv"
data = 'LOAD DATA LOCAL INFILE \'' + file_path        + '\'REPLACE INTO TABLE '        + table_name        + 'CHARACTER SET UTF8 FIELDS TERMINATED BY \','          '\' ENCLOSED BY \'\"\' '          'LINES TERMINATED BY \'\n\' IGNORE 1 LINES;'
cur.execute(data.encode('utf8'))

con.commit()  # 提交事务

关闭游标和数据库连接。
cur.close()
con.close()

csv数据导入样式:

mysql支持csv数据的导入,以下是sql的语法:

LOAD DATA INFILE '文件名'
REPLACE INTO TABLE 表名
CHARACTER SET UTF8
FIELDS TERMINATED BY ';'

ENCLOSED BY '"'
LINES TERMINATED BY '\n'

完整脚本:

import pymysql

# file_path = "exam.csv"
# table_name = 'update_time_table'
file_path = "export.csv"
table_name = "TBexport"
try:
    con = pymysql.connect(user="root",
                          passwd="root",
                          db="test",
                          host="47.95.20x.xxx",
                          local_infile=1)
    con.set_charset('utf8')
    cur = con.cursor()
    cur.execute("set names utf8")
    cur.execute("SET character_set_connection=utf8;")

    with open(file_path, 'r', encoding='utf8') as f:
        reader = f.readline()
        print(reader)
        devide = reader.split(',')  # 做成列表
        devide[-1] = devide[-1].rstrip('\n')  # 去除最后的换行符
        print(devide)

    column = ''
    for dd in devide:
        #如果标题过长,只能存成text格式
        if dd == "标题":
            column = column + dd + ' TEXT,'
        else:
            column = column + dd + ' varchar(255),'
    col = column.rstrip(',')  # 去除最后一个多余的,
    # print(column[:-1])
    create_table_sql = 'create table if not exists {} ({}) DEFAULT CHARSET=utf8'.format(table_name, col)
    print(create_table_sql)
    data = 'LOAD DATA LOCAL INFILE \'' + file_path + '\'REPLACE INTO TABLE ' + table_name + ' CHARACTER SET UTF8 FIELDS TERMINATED BY \',\' ENCLOSED BY \'\"\' LINES TERMINATED BY \'\n\' IGNORE 1 LINES;'
    cur.execute(create_table_sql)
    cur.execute(data.encode('utf8'))
    print(cur.rowcount)
    con.commit()
except:
    print("发生错误")
    con.rollback()

finally:
    cur.close()
    con.close()

csv文件快速转存到mysql

标签:读取   update   except   close   exce   提交   data   char   col   

原文地址:https://www.cnblogs.com/shaozheng/p/12019478.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!