Sometimes, you want to build the query string by yourself. Before 1.2.0, you can simply call conn.escape(any_object). However, it does not work anymore in 1.2.1
>>> import MySQLdb
>>> conn = MySQLdb.connect()
>>> conn.escape(5)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: no default type converter defined
The work around is to do
conn.escape(5, conn.encoders)
Is this a bug?
Thanks,
-Ted
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Sometimes, you want to build the query string by yourself. Before 1.2.0, you can simply call conn.escape(any_object). However, it does not work anymore in 1.2.1
>>> import MySQLdb
>>> conn = MySQLdb.connect()
>>> conn.escape(5)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: no default type converter defined
The work around is to do
conn.escape(5, conn.encoders)
Is this a bug?
Thanks,
-Ted
Why are you using escape at all? cursor.execute() does all escaping for you, if you use it correctly.
Let's say you had a table object and you wanted to return records that matched criteria that are passed to the object's call method, for instance.
In this case, the where clause would be of variable length and may include things like "IS NULL", etc.
The body of one such method that broke with the new escape included this:
wheres = []
for i in range(len(fields)):
if values[i] == None:
wheres.append("
%s
IS NULL" % (fields[i]))else:
wheres.append("
%s
= %s" % (fields[i], self.db.con.escape(values[i])))if len(wheres) > 0:
whereClause = "WHERE %s" % ' AND '.join(wheres)
else:
whereClause = ''
It seems like connection.escape is fully exposed for public use. Changing it was a big deal for me.
While it is possible to use execute in cases like this, this way might be more straightforward to some of us.
Kael
sorry that indentation is all messed up in that post