LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 09-21-2009, 04:29 AM   #1
threaderslash
Member
 
Registered: Dec 2008
Location: HongKong
Distribution: Fedora/Centos/RedHat
Posts: 109

Rep: Reputation: 15
Cool Python MySQL: interactive query input


Hello Everybody...

I have a query that works as follows:

Code:
db.query("""SELECT traveler.travelerFirstName,vaccine.vaccineName from
(traveler INNER JOIN takenvaccine ON traveler.travelerID = takenvaccine.travelerID)
        INNER JOIN vaccine ON takenvaccine.vaccineID=vaccine.vaccineID
        INNER JOIN requiredvaccine ON vaccine.vaccineID=requiredvaccine.requiredvaccineID
        INNER JOIN city ON requiredvaccine.cityID = city.cityID
WHERE traveler.travelerFirstName = 'John'""")
The output gives me all vaccines taken by a given employee. To allow the user to choose different names when running the system, I am trying to use a variable, named person:

Code:
person = "John"

db.query("""SELECT traveler.travelerFirstName,vaccine.vaccineName from
(traveler INNER JOIN takenvaccine ON traveler.travelerID = takenvaccine.travelerID)
        INNER JOIN vaccine ON takenvaccine.vaccineID=vaccine.vaccineID
        INNER JOIN requiredvaccine ON vaccine.vaccineID=requiredvaccine.requiredvaccineID
        INNER JOIN city ON requiredvaccine.cityID = city.cityID
WHERE traveler.travelerFirstName = 'person'""")
Then I run the query inside my python program. The first version without variable works fine. But the second, using variable, doesn't give me any output. What I am missing here about the sintax to make the MySQL work with variable ... Any suggestion?

All comments or suggestions are highly appreciated!

Last edited by threaderslash; 09-21-2009 at 04:31 AM.
 
Old 09-21-2009, 10:08 PM   #2
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Distribution: UBUNTU 5.10 since Jul-18,2006 on Intel 820 DC
Posts: 459

Rep: Reputation: 58
Since your code for john and person are identical, it probably looks for a person called 'person'.

Some variation of \{$person) is called for (I dont know python).

Please post the solution.

End
 
Old 09-21-2009, 10:26 PM   #3
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 98
Don't really know python either, but this link should give you the format you need:

http://atomboy.isa-geek.com/plone/Me...-python-module

HTH

Forrest
 
Old 09-22-2009, 12:41 AM   #4
threaderslash
Member
 
Registered: Dec 2008
Location: HongKong
Distribution: Fedora/Centos/RedHat
Posts: 109

Original Poster
Rep: Reputation: 15
Lightbulb solution

Thanks for all comments and suggestions.

Now it works! Here is the solution:

Code:
 
self.db = MySQLdb.connect(hostname,username,passwd,dbname)
self.cursor=self.db.cursor();

name="John"
runQuery="SELECT traveler.travelerFirstName,vaccine.vaccineName from traveler INNER JOIN takenvaccine ON traveler.travelerID = takenvaccine.travelerID INNER JOIN vaccine ON takenvaccine.vaccineID=vaccine.vaccineID INNER JOIN requiredvaccine ON vaccine.vaccineID=requiredvaccine.requiredvaccineID INNER JOIN site ON requiredvaccine.siteID = site.siteID WHERE traveler.travelerFirstName = %s"
          
self.cursor.execute(runQuery,(name,))        
        
print "tell vaccines taken for a chosen traveler\n"

for row in self.cursor.fetchall():
     print row
The term "self." is a object oriented syntax used in Python to denote a global variable that internally belongs to that class in use and is visible to all methods inside that object.

Note: you need to declare the whole query in the same line to it take effect in the variable "runQuery".
 
Old 09-22-2009, 06:36 PM   #5
threaderslash
Member
 
Registered: Dec 2008
Location: HongKong
Distribution: Fedora/Centos/RedHat
Posts: 109

Original Poster
Rep: Reputation: 15
Lightbulb

That is it! Looks better now...

Code:
self.db = MySQLdb.connect(hostname,username,passwd,dbname)
self.cursor=self.db.cursor();

name="John"
runQuery="""SELECT traveler.travelerFirstName,vaccine.vaccineName from
traveler INNER JOIN takenvaccine ON traveler.travelerID = takenvaccine.travelerID
        INNER JOIN vaccine ON takenvaccine.vaccineID=vaccine.vaccineID
        INNER JOIN requiredvaccine ON vaccine.vaccineID=requiredvaccine.requiredvaccineID
        INNER JOIN city ON requiredvaccine.cityID = city.cityID
WHERE traveler.travelerFirstName = %s"""
          
self.cursor.execute(runQuery,(name,))        
        
print "tell vaccines taken for a chosen traveler\n"

for row in self.cursor.fetchall():
     print row
To keep indentation in the query, just use triple """ when entering it to the variable/string runQuery.
 
  


Reply

Tags
member


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP - How to properly handle GET method input in a MySQL query? win32sux Programming 16 03-16-2008 12:20 PM
C interactive line input SciYro Programming 6 04-06-2007 01:57 PM
help with mysql query: return nth rows in query hawarden Programming 2 07-31-2006 06:36 PM
want perl interactive mode as python llmmix Programming 2 06-13-2006 05:43 AM
How to input several cmds to interactive apps through one shell cmd? kornerr Linux - General 4 01-01-2006 01:05 PM


All times are GMT -5. The time now is 02:56 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration