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?

有帮助吗?

解决方案

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,))
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top