1

My sqlite select query with where clause in python return none or empty (>>> )

import os.path
import sqlite3

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
db_path = os.path.join(BASE_DIR, "Dictionary.db")
with sqlite3.connect(db_path) as db:
    t = ('hello',)
    cursor = db.cursor()
    cursor.execute("SELECT * FROM entries Where word=?",t)
    Value = cursor.fetchall()
    for i in Value:
        print (i)        

Output: ( >>> )

But when I use simple select query without where clause it returns all data

5
  • Please paste the INSERTs, so that people can reproduce the behavior. Also, maybe you're just missing quotes around the word i.e.: SELECT * FROM entries Where word="?". Commented Sep 23, 2017 at 8:41
  • This is mentioned in sqlite official website Commented Sep 23, 2017 at 8:48
  • are you sure you have a row with word = 'hello' ? Commented Sep 23, 2017 at 9:01
  • What is the output of SELECT word='hello', * FROM entries? Does one row get marked with 1? Commented Sep 23, 2017 at 9:56
  • Yes I have because this is db of dictionary containing 176023 words Commented Sep 23, 2017 at 13:00

1 Answer 1

2

Try this query:

cursor.execute("SELECT * FROM entries Where word=?",(t,))

The second argument should be a tuple or a list.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.