我有304MB的文本文件,其中每行包含逗號(,)分隔值,我正在逐行閱讀文件並插入使用循環的值,但我不斷收到此錯誤後插入21行。 這裏是代碼在mysql表中使用for循環python插入值
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")