sqlite3.ProgrammingError:绑定参数1时出错:不支持类型“sqlite3.Row”

问题描述 投票:0回答:1

编辑:已修复。问题是我的可变条形码与其他条形码变量冲突。

我......很困惑,我找不到任何有关此错误的信息,尝试了所有现有的谷歌搜索技巧,仍然找不到解决方案。

所以我正在使用 Flask 制作一个连接到 SQLite 数据库的 API,我有一个函数可以从数量中删除 1:

import sqlite3
from flask import Flask, g

app = Flask(__name__)
database = './stock.db'

def get_db():
    db = getattr(g, '_database', None)
    if db is None:
        db = g._database = sqlite3.connect(database)
        db.row_factory = sqlite3.Row
    return db

@app.teardown_appcontext
def close_connection(exception):
    db = getattr(g, '_database', None)
    if db is not None:
        db.close()

def query_db(query, args=(), one=False):
    cur = get_db().execute(query, args)
    rv = cur.fetchall()
    cur.close()
    return (rv[0] if rv else None) if one else rv

def set_db(query, args=(), one=False):
    cur = get_db().execute(query, args)
    get_db().commit() # just bad. just just bad.
    # ignore comment before this one, its incorrect
    # actually it's probably correct
    cur.close()

@app.route('/')
def index():
    manual = open("stockmanual", "r")
    return manual.read()

@app.route('/remove/<int:barcode>', methods=['GET'])
def removequantity(barcode):
    barcode = query_db('SELECT * FROM stock WHERE Barcode = ?',
                              [barcode], one=True)
    
    if barcode is None:
        return 'null'

    set_db('''
    UPDATE stock
    SET Quantity = Quantity - 1
    WHERE Barcode = ?
    ''', [barcode])

但我收到此错误:

sqlite3.ProgrammingError: Error binding parameter 1: type 'sqlite3.Row' is not supported

我不再使用 stackoverflow,但我找不到任何相关内容。 其他一切都很好。此时我对此感到困惑,就像除了我之外没有人遇到过这个错误。

请帮忙。

完整错误消息:

Traceback (most recent call last):
  File "/usr/lib/python3.11/site-packages/flask/app.py", line 2190, in wsgi_app
    response = self.full_dispatch_request()
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/flask/app.py", line 1486, in full_dispatch_request
    rv = self.handle_user_exception(e)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/flask/app.py", line 1484, in full_dispatch_request
    rv = self.dispatch_request()
         ^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/flask/app.py", line 1469, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/lost/projects/ppddvvapi/stock.py", line 58, in removequantity
    set_db('''
  File "/home/lost/projects/ppddvvapi/stock.py", line 40, in set_db
    cur = get_db().execute(query, args)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
sqlite3.ProgrammingError: Error binding parameter 1: type 'sqlite3.Row' is not supported```
python sqlite flask
1个回答
0
投票

我认为真正的问题在于这个函数:

def query_db(query, args=(), one=False):
    cur = get_db().execute(query, args)
    rv = cur.fetchall()
    cur.close()
    return (rv[0] if rv else None) if one else rv

fetchall()
返回数据库行序列。所以
rv[0]
是一行;它不是您所期望的单一值。

© www.soinside.com 2019 - 2024. All rights reserved.