MySQLdb - parameterised SQL - how to see resulting SQL ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • shearichard@gmail.com

    #1

    MySQLdb - parameterised SQL - how to see resulting SQL ?

    Hi - I've got SQL that looks like this ...

    cursor =
    self.MySQLDb_co nn.cursor(curso rclass=MySQLdb. cursors.DictCur sor)
    sqlQuery = "SELECT * FROM T1 WHERE C1 = %s and C2 = %s"
    sql = cursor.execute( sqlQuery,(strUR LAlias,strSessi onID))
    rows = cursor.fetchall ()
    cursor.close

    .... i would be interested in seeing what the actual SQL used by the
    ..execute looks like after the replacements have been done. Is there a
    way of doing this ?

    thanks

    richard.

  • Dan Sommers

    #2
    Re: MySQLdb - parameterised SQL - how to see resulting SQL ?

    On 17 May 2006 18:41:55 -0700,
    shearichard@gma il.com wrote:
    [color=blue]
    > Hi - I've got SQL that looks like this ...
    > cursor =
    > self.MySQLDb_co nn.cursor(curso rclass=MySQLdb. cursors.DictCur sor)
    > sqlQuery = "SELECT * FROM T1 WHERE C1 = %s and C2 = %s"
    > sql = cursor.execute( sqlQuery,(strUR LAlias,strSessi onID))
    > rows = cursor.fetchall ()
    > cursor.close[/color]

    ITYM cursor.close()
    [color=blue]
    > ... i would be interested in seeing what the actual SQL used by the
    > .execute looks like after the replacements have been done. Is there a
    > way of doing this ?[/color]

    Taking a quick peek at pysqlite and the API spec, I'd say no, not a
    standard way.

    OTOH, eventually, mysqldb has to create that SQL in order to pass it to
    the database for execution, so it's probably as simple as finding the
    right place to put a "print" statement and/or to store the finished SQL
    in the cursor object.

    HTH,
    Dan

    --
    Dan Sommers
    <http://www.tombstoneze ro.net/dan/>
    "I wish people would die in alphabetical order." -- My wife, the genealogist

    Comment

    • Ben Finney

      #3
      Re: MySQLdb - parameterised SQL - how to see resulting SQL ?

      shearichard@gma il.com writes:
      [color=blue]
      > ... i would be interested in seeing what the actual SQL used by the
      > .execute looks like after the replacements have been done. Is there a
      > way of doing this ?[/color]

      I also think this would be a very useful feature for enabling logging,
      debugging, and other introspection. It surely is possible for the code
      to present that information (it must construct the final SQL statement
      to pass to the database engine), but I can't see a way to get at it
      with the current DB API.

      --
      \ "Better not take a dog on the space shuttle, because if he |
      `\ sticks his head out when you're coming home his face might burn |
      _o__) up." -- Jack Handey |
      Ben Finney

      Comment

      • Damjan

        #4
        Re: MySQLdb - parameterised SQL - how to see resulting SQL ?

        > ... i would be interested in seeing what the actual SQL used by the[color=blue]
        > .execute looks like after the replacements have been done. Is there a
        > way of doing this ?[/color]

        On my development machine I'm using the "--log" argument to MySQL so that it
        logs all queries and connections. I wouldn't recommend that on a production
        machine though.

        The logfile is stored in /var/lib/mysql/<hostname>.lo g


        --
        damjan

        Comment

        Working...