#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    6
    Rep Power
    0

    Unhappy mysqldb: execute


    Hi everybody,
    Suppose dbconn is a database connection.
    context = dbconn.cursor()

    binNumber = "123"

    context.execute("select decode(%s, 'pass')", (binNumber,))

    I don't understand how the above "select..." statement works? I know what decode() does. My problem is not the decode() function, I don't understand how the string formatting will be done thru % operator.
    If I connect to mysql and try to execute the above statement as it appears, I'll get an error message. However, executing this statement thru a Python script won't generate any error.
    I don't know why I don't have any % operator before 'binNumber'?
    How the above statement is being executed? What is the exact statement that being executed? How mysqldb translates the %s?

    Any help is greatly appreciated.
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Python doesn't demand that you do the string formatting at the same time as you assign the string to a variable, so you can store a string constining flags like %s for when you need them. So, what is actually happening is along these lines...

    Code:
    >>> n = '123'
    >>> s = 'just a simple string, %s, Ahhhhh!'
    >>> s % n
    'just a simple string, 123, Ahhhhh!'
    >>>
    All the execute() method is doing is taking the arguments for the string formatting as additional paramaters and preforming whatever action on them before inserting the values into the SQL query. Next comes execution and the results are returned .

    Hope this helps,

    Mark.
    Last edited by netytan; June 9th, 2004 at 10:06 AM.
    programming language development: www.netytan.com Hula

  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    6
    Rep Power
    0

    Exclamation Re:


    Dear Netytan,
    Thanks for the reply. What you wrote it's understandble.

    >>> n = '123'
    >>> s = 'just a simple string, %s, Ahhhhh!'
    >>> s % n
    'just a simple string, 123, Ahhhhh!'
    >>>

    What you wrote is equal:

    >>> 'just a simple string, %s, Ahhhhh!' %n
    >>> 'just a simple string, 123, Ahhhhh!'

    My point of confusion was that, in the string I have you do NOT see any % before 'binNumber':

    context.execute("select decode(%s, 'pass')", (binNumber,))

    So how this part is being executed?

    Am I right, or I am still missing a point?
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    OK i'll write you a small example function so you can see whats going on inside execute like functions .

    Code:
    #!/usr/bin/env python
    
    def printf(string, insert):
        print string % insert
    
    n = '123'
    s = 'put %s in to s..'
    
    printf(s, n)
    And its as simple as that, the actual string formating and the % operator are safly inside the execute() method so you never have to worry about them.

    Take care,

    Mark.
    programming language development: www.netytan.com Hula


IMN logo majestic logo threadwatch logo seochat tools logo