Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 8th, 2004, 01:16 PM
SkyIsBlue SkyIsBlue is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 6 SkyIsBlue User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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.

Reply With Quote
  #2  
Old June 9th, 2004, 10:04 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #3  
Old June 9th, 2004, 10:54 AM
SkyIsBlue SkyIsBlue is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 6 SkyIsBlue User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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?

Reply With Quote
  #4  
Old June 10th, 2004, 05:40 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > mysqldb: execute

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap