[Tutor] SQLite LIKE question

Alan Gauld alan.gauld at btinternet.com
Fri Apr 11 10:29:45 CEST 2008


"Dinesh B Vadhia" <dineshbvadhia at hotmail.com> wrote 

> I'm using the LIKE operator to match a pattern against a string 
> using this SELECT statement:
>
> for row in con.execute("
> SELECT <column> 
> FROM <table> 
> WHERE <string> LIKE '%q%' 
> limit 25"):

> With q="dog" as a test example, I've tried '$q%', '%q%', '%q' 

Ok, Thats the problem. The execute statement works 
somewhat like Python string formatting. Yopu don't put variable 
names in the string you put markers(which vary by databnase 
unfortunately!) In SqlLite they are question marks. You then 
follow the SQL statement with a list of values.

Here is an example from my tutorial topic on using databases:

book.execute('''DELETE FROM Address 
                      WHERE First LIKE ? 
                      AND Last LIKE ?''', (first,last) )

Notice the tuple at the end?

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld



More information about the Tutor mailing list