简体   繁体   中英

Passing variable into sql query in python (sqlite3)

I'm passing a value through into the query via a parameter. The problem is that when running the following code:

import sqlite3

def top10(restaurant):
    con = sqlite3.connect('example.db')
    cur = con.cursor()
    t = (restaurant,)
    cur.execute("SELECT * from orchard WHERE CUISINE_DESCRIPTION LIKE '%?%' ORDER BY SCORE;", t)
    return cur.fetchone()
print(top10("thai"))

I get the following error:

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

I'm not sure how what's triggering it. Here's what runs successfully when I embed a value called thai instead of passing in the variable:

cur.execute("SELECT * from orchard WHERE CUISINE_DESCRIPTION LIKE '%thai%' ORDER BY SCORE;")

What could be wrong?

试试这个

cur.execute("SELECT * from orchard WHERE CUISINE_DESCRIPTION LIKE ? ORDER BY SCORE;", '%'+t+'%')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM