0

I can't seem to find the proper way to do this. Basically, I want to search a database of people by name (whole or part).

Here's my setup:

name = str(raw_input("Enter name..."))

Assuming that the table is called people, and there is a column "name", how would I write this query using the library?

My attempt:

curs.execute("SELECT * FROM people WHERE people.name LIKE \"%:nm%\";", {"nm":name})

But it returns len(result) == 0 when I know the query with a fixed value between the wildcards returns something, so I'm not really sure what's going on here.

mgilson
  • 300,191
  • 65
  • 633
  • 696
  • IIRC, sqlite uses `?` style parameterization...`curs.execute('SELECT * FROM people WHERE people.name LIKE "%?%";', (name,))` – mgilson Oct 27 '16 at 17:33
  • @DanielRoseman I assume it's from the named placeholders feature [in first block of code](https://docs.python.org/2/library/sqlite3.html#cursor-objects), but I've never used it, plus the wildcard `%` – roganjosh Oct 27 '16 at 17:38
  • @mgilson perhaps you'll have better luck than me, I'm getting syntax error on the query trying to clean it up. The answer by Alex Martelli [here](http://stackoverflow.com/questions/3105249/python-sqlite-parameter-substitution-with-wildcards-in-like) suggests that your approach won't work either, even with a change in placeholder. But the proposed solution looks clunky too, having to concatenate the `%` into a string. – roganjosh Oct 27 '16 at 17:49
  • @mgilson You can use dictionaries too; with that said, it doesn't see the "?" for some reason and says "SQL takes 0 args, found 1". – smitty_werbermanjensen Oct 27 '16 at 17:56
  • @smitty_werbermanjensen have a look at the link I posted. I don't think the solution looks particularly elegant but that might be the only way – roganjosh Oct 27 '16 at 17:57
  • @roganjosh I just did, works perfectly. Thanks! – smitty_werbermanjensen Oct 27 '16 at 17:58

0 Answers0