In older versions of MySQLdb (I was using 0.2.2) the connection instance had an attribute named quote_conv. In 0.9.0 that seems to have disappeared. How do I get a dict to pass to MySQLdb.escape_sequence and friends?
Thx,
Skip
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Perhaps I should back up a step and ask if I'm doing this right. I have a list of 3-element tuples: (city,state,country). I'm building up a where clause like so:
for csc in need:
clause.append('city like "%s"'
' and state like "%s"'
' and country like "%s"' %
tuple(map(db.escape_string, csc)))
which I then connect with "or" and surround by parens to create one chunk of the where clause.
When I have to build up fragments of SQL instead of just calling c.execute with the appropriate string and tuple args, what's the correct way to escape the text the same way c.execute would? In 0.2.2 I was using MySQLdb.escape_row with the quote_conv dict as the second arg, which was why I asked about its replacement in 0.9.0.
Thx,
Skip
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The correct way is to use c.execute(), but if you can't do that for some reason, use db.literal(), which is what c.execute() uses. db.literal() operates on, and preserver, non-string sequences. Any string literals that are returned have the single-quotes already in them, so you don't need them in your SQL.
c.execute(query, args) effectively does query % db.literal(args).
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hmmm... Twice you've mentioned db.literal. I don't see it anywhere obvious. Given a connection instance:
>>> db
<MySQLdb.connections.Connection instance at 0x819e1ec>
if I type "db." and hit TAB twice, I see db.string_literal and db.escape_string as completion possibilities, but no db.literal. It's also not an attribute of MySQLdb or db._db.
MySQLdb reports the version as 0.9.0.
Thx,
Skip
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
In older versions of MySQLdb (I was using 0.2.2) the connection instance had an attribute named quote_conv. In 0.9.0 that seems to have disappeared. How do I get a dict to pass to MySQLdb.escape_sequence and friends?
Thx,
Skip
MySQLdb.converters.conversions
Don't call escape_sequence() directly. Instead call db.literal(). Normally if you have to do that, you're still doing something wrong.
Perhaps I should back up a step and ask if I'm doing this right. I have a list of 3-element tuples: (city,state,country). I'm building up a where clause like so:
for csc in need:
clause.append('city like "%s"'
' and state like "%s"'
' and country like "%s"' %
tuple(map(db.escape_string, csc)))
which I then connect with "or" and surround by parens to create one chunk of the where clause.
When I have to build up fragments of SQL instead of just calling c.execute with the appropriate string and tuple args, what's the correct way to escape the text the same way c.execute would? In 0.2.2 I was using MySQLdb.escape_row with the quote_conv dict as the second arg, which was why I asked about its replacement in 0.9.0.
Thx,
Skip
The correct way is to use c.execute(), but if you can't do that for some reason, use db.literal(), which is what c.execute() uses. db.literal() operates on, and preserver, non-string sequences. Any string literals that are returned have the single-quotes already in them, so you don't need them in your SQL.
c.execute(query, args) effectively does query % db.literal(args).
Hmmm... Twice you've mentioned db.literal. I don't see it anywhere obvious. Given a connection instance:
>>> db
<MySQLdb.connections.Connection instance at 0x819e1ec>
if I type "db." and hit TAB twice, I see db.string_literal and db.escape_string as completion possibilities, but no db.literal. It's also not an attribute of MySQLdb or db._db.
MySQLdb reports the version as 0.9.0.
Thx,
Skip
I guess it's in 0.9.1 and not 0.9.0. 0.9.1c2 *will* be released as 0.9.1 final in the very near future, and it has some important bug fixes.