Menu

Why escape() works differently in 1.2.1

Help
Ted Cui
2006-07-07
2012-09-19
  • Ted Cui

    Ted Cui - 2006-07-07

    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

     
    • Andy Dustman

      Andy Dustman - 2006-07-07

      Why are you using escape at all? cursor.execute() does all escaping for you, if you use it correctly.

       
      • Kael Fischer

        Kael Fischer - 2006-09-09

        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

         
        • Kael Fischer

          Kael Fischer - 2006-09-09

          sorry that indentation is all messed up in that post

           

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.