Menu

Viewing the final SQL?

Help
2005-10-09
2012-09-19
  • Peter Bowyer

    Peter Bowyer - 2005-10-09

    I'm just starting with Python, and would like to know how I can view the SQL after interpolation? I'm fighting this error message:

    Traceback (most recent call last):
    File "feedsloader.py", line 151, in ?
    main()
    File "feedsloader.py", line 145, in main
    feeder.process(feed)
    File "feedsloader.py", line 77, in process
    (etag, modified, data.status, feed['feed_id']))
    File "/usr/lib/python2.4/site-packages/MySQLdb/cursors.py", line 137, in execute
    self.errorhandler(self, exc, value)
    File "/usr/lib/python2.4/site-packages/MySQLdb/connections.py", line 33, in defaulterrorhandler
    raise errorclass, errorvalue
    _mysql_exceptions.OperationalError: (1205, 'Lock wait timeout exceeded; Try restarting transaction')

    despite not using transactions or threading. I am hoping that viewing the SQL as the database sees it may give some pointer to the problem.

     
    • Eric Huss

      Eric Huss - 2005-12-04

      Would you be willing to expose a method that would provide a string after the interpolation? In my case, I'd rather do that than do it at query-time. I actually just want to call connection.literal(), but the docstring says not to do that. ;)

       
    • Peter Bowyer

      Peter Bowyer - 2005-10-10

      I fixed the error (I hadn't realised I needed to commit() the changes, as I've never needed to in PHP), but the original question still stands.

       
      • Andy Dustman

        Andy Dustman - 2005-10-10

        PHP was likely using auto-commit mode, or using non-transactional tables, so you weren't getting transactions at all.

        There's not a built-in way to see the raw SQL statements before they are issued. However, you could probably do something like this (untested and pre-coffee):

        class DebugCursor(MySQLdb.Cursor):

        def _query(self, q):
            import sys
            print >> sys.stderr, q
            return self._do_query(q)
        

        ...

        c = db.cursor(DebugCursor)

        perform query as normal

        If you aren't getting exceptions, you can also just look at c._executed for the last raw executed query.

         

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.