quoting %

M

Matthias Teege

Moin,

I need a string like this "foo '%value%'" for a database query but
print "'%%s%'"%value gives me TypeError: not all arguments converted
during string formatting. How does I quote the "%" sign in this case?

Matthias
 
T

Thomas Guettler

Am Wed, 13 Oct 2004 13:32:38 +0200 schrieb Matthias Teege:
Moin,

I need a string like this "foo '%value%'" for a database query but
print "'%%s%'"%value gives me TypeError: not all arguments converted
during string formatting. How does I quote the "%" sign in this case?

Hi,

%% --> %
 
R

Richie Hindle

[Matthias]
I need a string like this "foo '%value%'" for a database query but
print "'%%s%'"%value gives me TypeError: not all arguments converted
during string formatting. How does I quote the "%" sign in this case?

Double up the percent signs:
foo '%spam%'
 
C

Cliff Wells

Moin,

I need a string like this "foo '%value%'" for a database query but
print "'%%s%'"%value gives me TypeError: not all arguments converted
during string formatting. How does I quote the "%" sign in this case?

You need to escape the extra percents:

print "'%%%s%%'" % value

Regards,
Cliff
 
A

Andreas Kostyrka

Moin,

I need a string like this "foo '%value%'" for a database query but
print "'%%s%'"%value gives me TypeError: not all arguments converted
during string formatting. How does I quote the "%" sign in this case?
Try: "'%%%s%%'"
And if your not completly sure what type value might be, it's better
to use str % (value,) (Hint: what happens if type(value) == tuple)?

Andreas
 
D

Dan Sommers

Try: "'%%%s%%'"

Be careful with the quotation marks, too; consider the case that value
contains a quotation mark (or an apostrophe).

I don't know what kind of database and/or API you're using, but DB-API
compliant (Python PEP 249) libraries will put the right quotation marks
(if any) around your strings *and* quote any internal troublesome
characters correctly.

HTH,
Dan
 
G

Gandalf

I don't know what kind of database and/or API you're using, but DB-API
compliant (Python PEP 249) libraries will put the right quotation marks
(if any) around your strings *and* quote any internal troublesome
characters correctly.
Can you please send an example? I'm using the DB-API extensively, but I
have my own conversion
functions for date/time types. It would be great to have a general method.

Laci 2.0
 
D

Dan Sommers

Can you please send an example? I'm using the DB-API extensively, but
I have my own conversion functions for date/time types. It would be
great to have a general method.

Here's a snippent from one of my programs:

def delete( self, tablename, column_names, row ):
cursor = self.connection.cursor( )
sql = ("DELETE FROM %s WHERE " % tablename
+ column_names[ 0 ] + " = %s")
params = tuple( row[ :1 ] )
cursor.execute( sql, params ) # <-- look here
self.connection.commit( )

By the time it gets to the "look here" line, sql looks like this:

DELETE FROM tablename WHERE columnname = %s

(but tablename and columnname are actual table and column names). Note
that cursor.execute quotes the rest of the WHERE clause *correctly*,
even if params contains quote or percent or whatever characters.

FWIW, I would probably be a bit more bold nowadays, and construct sql
and params more like this:

sql = "DELETE FROM %(table)s WHERE %(column)s = %(target)s"
params = { 'table' : tablename,
'column' : column_names[ 0 ],
'target' : row[ :1 ] }

assuming that the database module in question supports the pyformat
paramstyle, or like this:

sql = "DELETE FROM %s WHERE %s = %s"
params = (tablename, column_names[ 0 ], row[ :1 ])

if it didn't.

See also PEP 249 <http://www.python.org/peps/pep-0249.html>, especially
the bit about paramstyle and cursor.execute (and footnotes 2 and 5).

HTH,
Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top