以下是我的代码,用于在mysql表中加载数据(信息的7列):
#!/usr/bin/env python
import mysql.connector, csv, sys
csv.field_size_limit(sys.maxsize)
cnx = mysql.connector.connect(user='testuser', password= 'testuser', database='database')
cursor = cnx.cursor()
table=csv.reader(file("logs.txt"), delimiter='\t')
for row in table:
cursor.execute("INSERT INTO first_table (column1, column2, column3, column4 , column5, column6, column7) VALUES (%s, %s, %s, %s, %s, %s, %s)", row)
cnx.commit()
cnx.close()
以下是logs.txt文件组成的截断内容,其中7列(制表符分隔),最后一列(mouse_phene_id)可能为空,或者单个或多个项目以空格分隔:
human_gene_symbol entrez_id homolog_id hgnc_assoc mouse_gene_symbol mouse_mgi_id mouse_phene_id
A1BG 1 11167 yes A1bg MGI:2152878
A1CF 29974 16363 yes A1cf MGI:1917115 MP:0005387 MP:0005386 MP:0005388 MP:0005385 MP:0002873 MP:0010768 MP:0005369 MP:0005376 MP:0005384 MP:0005378
A2M 2 37248 yes A2m MGI:2449119
A3GALT2 127550 16326 yes A3galt2 MGI:2685279
A4GALT 53947 9690 yes A4galt MGI:3512453 MP:0005386 MP:0010768 MP:0005376
A4GNT 51146 87446 yes A4gnt MGI:2143261 MP:0005384 MP:0002006 MP:0005385 MP:0005381 MP:0005387
AAAS 8086 9232 yes Aaas MGI:2443767 MP:0005389 MP:0005386 MP:0005378
AACS 65985 11322 yes Aacs MGI:1926144
AADAC 13 37436 yes Aadac MGI:1915008
我收到以下错误,并且由于这是一个常见错误,因此我尝试了与该错误相关的在stackoverflow上发布的所有内容,但仍未修复和卡住:
Traceback (most recent call last):
File "insert_mysql.py", line 23, in <module>
cursor.execute("INSERT INTO first_table (column1, column2, column3, column4 , column5, column6, column7) VALUES (%s, %s, %s, %s, %s, %s, %s)", row)
File "/usr/local/lib/python2.7/dist-packages/mysql/connector/cursor.py", line 551, in execute "Not all parameters were used in the SQL statement")
mysql.connector.errors.ProgrammingError: Not all parameters were used in the SQL statement
first_table:
非常感谢任何帮助和建议。先感谢您。
最佳答案
您所看到的错误很可能是由于logs.txt
文件中的一行所引起的,该行的项目数量(7)与插入准备语句所期望的数量不相同。错误
并非在SQL语句中使用了所有参数
表示您有一个或多个无法分配值的位置参数,因此未使用。
我可以对问题可能在文件中的位置提出两个建议。首先,您的日志文件可能具有一个或多个标题行,这些标题行可能没有脚本期望的相同列数或值类型。您可以通过以下方式跳过任意数量的初始行:
skip_first_line = next(table)
skip_second_line = next(table)
# etc.
换句话说,只需调用
next()
即可消耗日志文件中任意数量的不包含您要插入的实际数据的初始行。如果不是,那么可能有问题的行就在文件中间。您可以尝试打印脚本中的每一行,以查看其死处:
for row in table:
cursor.execute("INSERT INTO first_table (column1, column2, column3, column4 , column5, column6, column7) VALUES (%s, %s, %s, %s, %s, %s, %s)", row)
print row
关于python - mysql插入代码产生以下错误:并非在SQL语句中使用所有参数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47936097/