Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python sqlite user input search

I’m trying to get the IDs of the films from DB where category equals users input category, but for some reason I’m not able to do it.
My code:

import sqlite3

#Connectin to DB
conn = sqlite3.connect('sakila.db')
c = conn.cursor()

#Checking if the connection to the DB is successful
if (conn):
    print("Connection successful")
else:
    print ("Connection unsuccessful")

kategorija=input("Enter the category: ")
c.execute("SELECT FID FROM film_list WHERE category=%s", (kategorija,))
film = c.fetchall()
#Removing duplicates
final_film = list(dict.fromkeys(film))
print(final_film)

The error i get: line 14, in
c.execute("SELECT FID FROM film_list WHERE category=%s", (kategorija,))
sqlite3.OperationalError: near "%": syntax error

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

As you can get here the way to bind python variables to sqlite queries is ?

priority = input("Enter priority : ")
cur = conn.cursor()
cur.execute("SELECT * FROM tasks WHERE priority=?", (priority,))
rows = cur.fetchall()

If you want to remove duplicates you could consider changing your query to

SELECT DISTINCT ...

This will change the behavior of sql to only select distinct values (i think this is obvious).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading