Question

I am trying to parametrize a subquery in python using sqlite, but keep running into syntax errors. My query is the following,

cur.execute("SELECT Name FROM names WHERE SectionCode = (SELECT SectionCode FROM names WHERE Key = ?, (tempemail,))")

Essentially, I want to get a list of all the names of people who have the same section as the person with the key which is stored in the variable tempemail. I've tried not parametrizing the query as well but I then run into different errors. The queries individually seem to work, but not when nested as above. Can anyone offer any guidance?

Was it helpful?

Solution

Anything inside the quotes is the SQL statement; anything outside is Python code.

The parameter you want to give to the execute function is a Python variable:

cur.execute("SELECT ... = (SELECT ... WHERE Key = ?)", (tempemail,))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top