The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
mysqldb: execute
Discuss mysqldb: execute in the Python Programming forum on Dev Shed. mysqldb: execute Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

June 8th, 2004, 01:16 PM
|
|
Registered User
|
|
Join Date: Jun 2004
Posts: 6
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
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.
|

June 9th, 2004, 10:04 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
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.
__________________
programming language development: www.netytan.com – Hula
Last edited by netytan : June 9th, 2004 at 10:06 AM.
|

June 9th, 2004, 10:54 AM
|
|
Registered User
|
|
Join Date: Jun 2004
Posts: 6
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
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?
|

June 10th, 2004, 05:40 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|