mysql - Python - Multiple SQL queries within code -


i creating python script receives input (13 digit number). takes number , checks if it's in database (to check whether person has registered). after that, needs check whether person logged in or not (in database, have column called 'status' either 0 or 1). can't, life of me, figure out how can second query within code.

i using python 2.7 mysqldb module.

see code below:

import mysqldb

# create database connection db = mysqldb.connect(host="******", user="******", passwd="*****", db="******") cur = db.cursor()  # create query select ids cur.execute("select id, users") clientarray = []  # loop on ids returned query, # save ids in clientarray row in cur.fetchall():     clientid = str(row[0])     clientarray.append(clientid) print clientarray #printing troubleshooting, ignore   # loop infinitely until - when receive input (which clientid) # check if clientid exists in clientarray.  # if clientid exists, check see whether client has been checked in # if client checkin returns false, check client in - turn on green led light # if client checkin returns true, check client out - turn on red light  # if clientid not exist in clientarray, turn on red , green led lights  clientidinput = "" while true:     clientidinput = raw_input("")     if clientidinput in clientarray:         print "the id has been found in database"         # check see whether person has been logged in or not          ### code not work, can't figure part out ###         cur.execute("select status users id='clientidinput'")         clientstatus = cur.fetchall()         if clientstatus == true:             print "this person checked in. checking out now."              # update mysql database , change status 1 0         if clientstatus == false:             print "this person has not checked in yet. checking in now."             # update mysql database , change status 0 1             else:         print " id has not been found in database" 

you need parameterize query , pass clientidinput variable value it. then, use fetchone() results:

cur.execute("select status users id=%s", (clientidinput, )) data = cur.fetchone() if not data:     print "the id has not been found in database" else:     status = data[0]     if status:         print "this person checked in. checking out now."          # update mysql database , change status 1 0     else:         print "this person has not checked in yet. checking in now." 

hope understood use case , logic correctly.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -