这是我的代码
#!usr/bin/python
from __future__ import print_function
import sqlite3
import os, sys, subprocess
import numpy as np
from Bio import Entrez
from Bio import Medline
Entrez.email = "shayezkarimcide@gmail.com"
handle = Entrez.esearch(db="pmc",
term = "Antimicrobial resistance",
rettype = "medline",retmode = "txt",
retmax= '10',sort = "pub date")
result = Entrez.read(handle)
handle.close()
Id = result ['IdList']
print (list(Id), "\n")
rint ("The length of PubId is :",len(Id))
conn = sqlite3.connect('/home/cbbi-l2-16/Desktop/karim')
c = conn.cursor()
print ("Opened database successfully")
c.executemany("INSERT INTO Entrez (PuId) VALUES (?)", Id)
for row in c :
print (row)
conn.commit()
print ("Records Save Successfully")
conn.close()?
它给出错误
文件“sqlpython.py”,第 42 行,在
c.executemany(“插入Entrez(PuId)值(?)”,Id)
sqlite3.ProgrammingError:提供的绑定(bind)数量不正确。当前语句使用 1,提供了 7 个。
最佳答案
只需添加 ?
与 Id
中的参数一样多.
根据 sqlite3 文档:
Put ? as a placeholder wherever you want to use a value, and then provide a tuple of values as the second argument to the cursor’s execute() method.
根据文档,
executemany
中应该是这种情况同样,看看这个取自文档的例子:Larger example that inserts many records at a time
purchases = [('2006-03-28', 'BUY', 'IBM', 1000, 45.00),
('2006-04-05', 'BUY', 'MSFT', 1000, 72.00),
('2006-04-06', 'SELL', 'IBM', 500, 53.00), ]
c.executemany('INSERT INTO stocks VALUES (?,?,?,?,?)', purchases)
关于python - 如何将python连接到sqlite3并一次填充多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52700926/