python - sqlite3.ProgrammingError : Incorrect number of bindings supplied. 当前语句使用1,并且提供了4个

标签 python sqlite

def get_stock(item_url):
    source_code = requests.get(item_url)
    plain_text = source_code.text
    soup = BeautifulSoup(plain_text, features="html.parser")

    for link in soup.findAll('a',{'class':'none'}):
        words=link.string
        stock_num=words[1:5]


        if stock_num.isdigit():
            href='https://tw.stock.yahoo.com/q/q?s='+ stock_num
            print(stock_num)
            c.execute('insert into stocks(stocknum) values (?)',stock_num)
            conn.commit()

我正在尝试将四位数字符串 stock_num 插入到我的 SQLite 中。 但是,它显示

sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 4 supplied.

下面是我创建表的代码

CREATE TABLE "stocks" (
    "stocknum"  TEXT NOT NULL,
    PRIMARY KEY("stocknum")
);

我不知道如何调整我的 table 或爬虫。 我已经研究了好几个小时了,但我不明白发生了什么。

最佳答案

您需要在元组中传递参数 - 如果只有一个元组,则为一元组,但仍然是一个元组。

错误消息源于字符串 stock_num 恰好有 4 位数字,因此 sqlite 包装器会解析例如: “1337”“1”“3”“3”“7” .

要解决此问题,

c.execute('insert into stocks(stocknum) values (?)', stock_num)

应该是

c.execute('insert into stocks(stocknum) values (?)', (stock_num,))

关于python - sqlite3.ProgrammingError : Incorrect number of bindings supplied. 当前语句使用1,并且提供了4个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62572052/

相关文章:

python - 在 3d 数组上同时使用切片索引和 bool 索引时出现奇怪的结果

python - 将 NumPy 矩阵的不同值部分的上三角和下三角提取到 2 列 pandas

python - 如何列出 celery 中的排队项目?

python - TensorFlow v2 替换 clip_gradients_by_norm

Sqlite 按日期分组获取该日期的最新行值

android - 删除记录时Android崩溃

javascript - 我如何在 SQLite 中进行高级查询以按标签搜索文件?

python - python 交互式词典程序

android - 安卓应用数据库

sqlite - 在 sqlite3 中打开数据库并查看表?