简体   繁体   中英

cursor.execute() returning empty list when used with parametrized query(python,sqlite)

when i use this code:

 def recherche(case,value='*'):
        cursor = sqliteConnection.cursor()
        if value == '*':
            cursor.execute("SELECT * FROM Articles ORDER BY ID;")
        else:
            value="%" + str(value) + "%"
            p=(case,value)
            cursor.execute("SELECT * FROM Articles WHERE ? LIKE ? ORDER BY ID;",p)
        result = cursor.fetchall()
        cursor.close()
        return result

result returns as an empty list
on the other hand if i use this:

 def recherche(case,value='*'):
        cursor = sqliteConnection.cursor()
        if value == '*':
            cursor.execute("SELECT * FROM Articles ORDER BY ID;")
        else:
            value=str(value)
            p=(case,value)
            cursor.execute("SELECT * FROM Articles WHERE " + case + " LIKE '%" + value + "%' ORDER BY ID;")
        result = cursor.fetchall()
        cursor.close()
        return result

the values are returned properly.
From what i understood using the first method is more optimised but it doesnt work for me at all.

The LIKE? should be:

LIKE '?'

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