Search code examples
pythonsqliteflask

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


Edit: Fixed. Problem was that my variable barcode, was conflicting with the other barcode variable.

I'm... so confused, I can't find ANYTHING about this error, tried all the google search tricks that exist, still can't find a fix.

So I'm making a API using Flask that connects to a SQLite Database, I have a function to remove 1 from the quantity:

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])

But I get this error: sqlite3.ProgrammingError: Error binding parameter 1: type 'sqlite3.Row' is not supported

I don't use stackoverflow anymore but I just can't find anything on this. Everything else works fine. At this point I'm just confused about this, it's like no one has ever gotten this error except me.

Please help.

Full error message:

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```

Solution

  • I think the real problem is in this function:

    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() returns a list of one or more database rows that matched the query, where each row is itself also a list of the column values.

    So rv[0] is the first result row from the query. It is not a single value as you seem to expect.

    If you expect the query to return exactly one row, use fetchone() which returns a single row instead of fetchall() which returns a list-of-rows (even if it's a list of only one row).