python - 如何将python连接到sqlite3并一次填充多行

标签 python sqlite

这是我的代码

#!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/

相关文章:

java - Android 中为每个电话号码添加 1 位数字

sqlite - SQLite 中的 "insert"是否返回 SQLITE_OK 或 SQLITE_DONE?

python - 当模块名称中包含 '-' 破折号或连字符时如何导入模块?

python - Keras:在生成器中加载图像与在生成器外部加载图像

python - Pylance:在这种情况下不允许使用 "ClassVar"?

Python 和 os.chroot

sql - Django 似乎错误地声称存在 SQL 语法错误

javascript - WebSQL : SQLite Query Returns a Transaction Error

用于文本文件中值的 Python 拆分函数

database - SQLite 的 QSqlQuery 在前向迭代中使用 next() 只会找到一行