Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
275 views
in Technique[技术] by (71.8m points)

python - Updating a table with a sum of another table throws Incorrect number of bindings supplied error

I created a refresh database button in my web/flask form, but when I try to refresh, I get this error message:

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

I tried all the various combinations in cursor.execute(query, (database,)) but I just couldn't figure out how to fix it...

#refresh portfolio stats
@app.route('/refresh', methods = ['GET','POST'])
def refresh():
    if request.method == 'POST':
        database = "data.sqlite"
        connection = sql.connect(database)

        query = ''' UPDATE
                    portfolio_table
                    SET
                    portfolio_cost_total = (SELECT SUM(product_cost_total)
                                            FROM product_table)
                    WHERE
                    portfolio_table.portfolio_id = '1' '''

        cursor = connection.cursor()
        cursor.execute(query, (database,))

        db.session.commit()
        #df = pd.read_sql_query(query, connection)
        #df.head()

    return redirect(url_for('index'))
question from:https://stackoverflow.com/questions/65839701/updating-a-table-with-a-sum-of-another-table-throws-incorrect-number-of-bindings

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

"bindings" are the placedholders in a query. There are no placeholders in query (either ? or :name), but parameters are being sent to the execute call ie (database, ).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...