%s place holder does not let me insert ' in an sql query with python.

Paul Boddie paul at boddie.org.uk
Mon Dec 15 10:34:06 EST 2008


On 15 Des, 14:46, Krishnakant <hackin... at gmail.com> wrote:
> hello all,
> thanks for all of your very quick responses.
> The problem is that I am using python 2.5 so the 2.6 syntax does not
> apply in my case.

The parameter syntax for database operations is defined by the DB-API,
and this is a very different matter to that of Python string
substitution (or formatting). See this document for details:

http://www.python.org/dev/peps/pep-0249/

You should note that when sending the SQL command to the database
system, you do not use the % operator to prepare that command.
Instead, you should pass the collection of parameters as the second
argument of the execute method. In other words...

cursor.execute(query, parameters)

Note that the query is kept as a separate argument; you do not combine
these two things yourself.

Paul

P.S. As far as I know, Python 2.6 still has the traditional printf-
style substitution syntax, so any exhortation to adopt new-style
formatting is ill-advised, especially since it has only slight
relevance to this particular enquiry.



More information about the Python-list mailing list