如何将python连接到sqlite3并在上填充多行

2024-06-20 15:05:19 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的密码

#!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:提供的绑定数不正确。当前语句使用1,提供了7个。你知道吗


Tags: 文件fromimportidcloseentrezresultconn
1条回答
网友
1楼 · 发布于 2024-06-20 15:05:19

只需添加?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)

相关问题 更多 >