Reputation: 291
I'm trying to execute a query using pyodbc with this kind of code
cursor.execute("SELECT x from y where Name='%s'"%namepar)
The parameter may have a quote and so it needs to be escaped in order to work, how do i do thos? I tried by simply replacing " ' " with " \\' " in namepar and it still doesn't work, I get a pyodbc.ProgrammingError
Upvotes: 4
Views: 8378
Reputation: 1605
You could also try putting an extra set of quotes around it. It worked in my use case.
cursor.execute("SELECT x from y where Name=''%s''" % namepar)
Upvotes: -4
Reputation: 369074
You can pass parameters, and that will be escaped.
cursor.execute("SELECT x from y where Name = ?", (namepar,))
http://www.python.org/dev/peps/pep-0249/#id15
http://code.google.com/p/pyodbc/wiki/Cursor
Upvotes: 6