我有 304MB 的文本文件,其中每行包含逗号(,)分隔值,我正在逐行读取文件并使用 for 循环插入值,但在插入 21 行后我不断收到此错误。这是代码
import MySQLdb
myfile = open('/home/vaibhav/Desktop/question1.csv', 'r')
db = MySQLdb.connect(host="localhost",
user="root",
passwd="admin",
db="question1agg")
for line in myfile:
my_line_list = line.split(',')
string = ''
for value in my_line_list:
string = string + "'" + value + "',"
query_string = string[:-1]
final_query = "insert into question1 values"+"("+query_string+");"
cur = db.cursor()
cur.execute(final_query)
db.commit()
db.close()
这是我得到的错误
Traceback (most recent call last):
File "da.py", line 19, in <module>
cur.execute(final_query)
File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 174, in execute
self.errorhandler(self, exc, value)
File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
_mysql_exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 's Solids','Men','NULL','Solid','/Products/Legwear','/Products/Legwear/AmericanEs' at line 1")