我编写了以下代码,用于使用python将.csv文件中的大量地震记录数据转储到mysql表中:
import MySQLdb
import os
import string
db = MySQLdb.connect (host="111.100."123.134",port=3306,user="root",\
passwd="****",db="mydb")
print "\nConnction to DB established\n"
cursor=db.cursor()
sql = """LOAD DATA LOCAL INFILE 'all_week.csv' \
INTO TABLE Eq_records \
FIELDS TERMINATED BY ',' \
OPTIONALLY ENCLOSED BY '"' \
LINES TERMINATED BY '\r\n' \
IGNORE 1 LINES;;"""
try:
cursor.execute(sql)
db.commit()
print "Data loading complete.\n"
except:
print "Error in loading data\n"
此代码执行正常,但mysql工作台中的表Eq_records
为空。请让我知道解决方案。
答案 0 :(得分:0)
我执行了相同的代码并将数据导入MySQL表。我刚刚断开客户端连接并再次连接它并刷新数据库并且它工作正常。