我的程序有問題。我想從文件txt輸入數據庫。這是我的源代碼沒有足夠的格式字符串參數python mysql
import MySQLdb
import csv
db=MySQLdb.connect(user='root',passwd='toor',
host='127.0.0.1',db='data')
cursor=db.cursor()
csv_data=csv.reader(file('test.txt'))
for row in csv_data:
sql = "insert into `name` (`id`,`Name`,`PoB`,`DoB`) values(%s,%s,%s,%s);"
cursor.execute(sql,row)
db.commit()
cursor.close()
運行該程序後,在這裏的錯誤
Traceback (most recent call last):
File "zzz.py", line 9, in <module>
cursor.execute(sql,row)
File "/home/tux/.local/lib/python2.7/site-packages/MySQLdb/cursors.py", line 187, in execute
query = query % tuple([db.literal(item) for item in args])
TypeError: not enough arguments for format string
,這是我的test.txt
4
zzzz
sby
2017-10-10
請幫幫忙,並在此先感謝。
那麼,你的'行'看起來像什麼?它沒有足夠的元素 –
你可以給我看一個'test.txt'文件的樣子嗎? – skr
welp,我解決了它。感謝兄弟,你提醒我關於我的test.txt –