1.今天尝试从csv文件中(或是其它文本文件中)读取数据让后插入MySQL数据库中。我本来是想读一条然后插入一条的,结果200万的数据插了9个小时竟然直插入了70万左右,一想不行便使用先读出10000条数据将其存入list中,然后采用:
insert into table values(row1, row2, ...)
的方法进行插入处理,竟然90秒就完成了200万条数据的插入工作。
附上代码:
import csv
import time
from DateBase.connect_DB import connect_db
def insert_user_infor():
db = connect_db()
cursor = db.cursor()
file_path = './ceshi.csv'
with open(file_path, encoding='utf-8') as file_read:
lines = csv.reader(file_read)
for line in lines:
name = line[1]
gender = line[2]
location = line[3]
uid = line[4]
sql = "insert into user_infor_copy values('%s', '%s', '%s', '%s')" % (name, gender, location, uid)
try:
cursor.execute(sql)
db.commit()
except:
db.rollback()
def main():
time1 = time.time()
print(ti

最低0.47元/天 解锁文章
3308

被折叠的 条评论
为什么被折叠?



