|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
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. |
|
#3
|
|||
|
|||
|
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? |
|
#4
|
||||
|
||||
|
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. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > mysqldb: execute |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|