Python 3.4 - CGI - how to include variable in sql Select

I have an sql query function below which works if I insert a value for memberID (eg 4528). However it does not work if I insert the variable name memberID. The variable memberID is obtained from the function getData()  - see below - and this is functioning correctly.

I do not understand the syntax required to insert the variable memberID into the sql function.

Tks for help





def SelectfromDB(db,cursor):
    sql = "select T1ytdlatest,T2ytdlatest,T3ytdlatest from registrations  WHERE memberID = memberID"
    cursor.execute(sql)
    order=cursor.fetchall()
    return order 

Open in new window


def getData():
    form = cgi.FieldStorage()
    memberID = form.getvalue('memberID')
    return memberID

Open in new window

jameskaneAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
This page http://dev.mysql.com/doc/connector-python/en/connector-python-example-cursor-select.html shows the syntax using the MySQL Python connector.  You have to use '%s' (for strings) as placeholders so you can substitute the variables in the 'execute' statement.  I do not believe you can use the variable name directly in the SQL statement.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jameskaneAuthor Commented:
Thanks for the pointer Dave - that has helped, but I'm not quite there yet.  I now have the following modified select function and that works. You will note that while I am using memberID as a variable in the select process, I have inserted a value for it 1212 just before the sql.

So, the outstanding problem  is how to pick up that value of memberID from the getData() function shown below and feed it to the select function.  I have tried


if __name__== "__main__":
    try:
        htmlTop()
       memberID=getData()
      order=SelectfromDB(db,cursor)
     ..........
      .........
        htmlTail()



-- ie feeding the SelectfromDB(db,cursor) function with  memberID value generated by the getData() fuction. see modified SelectfromDB(db,cursor) below. The select query does not work in this case.

Any obvious error jump out  at you ?

Thanks very much for the help.

James


Select Function with memberID assigned value - THIS WORKS
def SelectfromDB(db,cursor):   
      memberID = "1212"
    cursor.execute("""select T1ytdlatest,T2ytdlatest,T3ytdlatest from registrations WHERE memberID = %s""" 
    %(memberID)) 
    order=cursor.fetchall()
    return order

Open in new window


Select Function with memberID  value from the getData() function THIS DOES NOT WORK
def SelectfromDB(db,cursor):   
    memberID = "memberID"
    cursor.execute("""select T1ytdlatest,T2ytdlatest,T3ytdlatest from registrations WHERE memberID = %s""" 
    %(memberID)) 
    order=cursor.fetchall()
    return order

Open in new window



def getData():
    form = cgi.FieldStorage()
    memberID = form.getvalue('memberID')
    return memberID

Open in new window

0
Dave BaldwinFixer of ProblemsCommented:
Confusing.  This... memberID = "memberID" assignes the string "memberID" to the variable memberID.
0
jameskaneAuthor Commented:
ah... found the answer

memberID = ('"%s"'%memberID)

thanks for the pointer.
0
Dave BaldwinFixer of ProblemsCommented:
You're welcome, glad to help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Python

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.