Reputation: 13
So, I am a rookie MySQL guy and a basic knowledge Python guy. Any help is much appreciated in advance. I have searched for specific threads related to this but have come up blank. The issue: I have a table that I want to be able to type in a first name and last name and if they match any records then return the data in the row, and the name of the table. Sounds so simple and probably is for a jedi, however, I am getting a TypeError:not all arguments converted during string formatting
My code:
import pymysql
user_input_fname = input("Please enter client first name and press Enter button: ")
user_input_lname = input("Please enter client last name and press Enter button: ")
db = pymysql.connect(host='127.0.0.1', port=44420, user='*****', passwd='*************', db='************')
cursor = db.cursor()
cursor.execute("""SELECT Named_Insured_First_Name, Named_Insured_Last_Name FROM condo_rented_to_others WHERE Named_Insured_First_Name='user_input_fname' AND Named_Insured_Last_Name='user_input_lname' """, (user_input_fname, user_input_lname,))
# calls fetchone until None is returned (no more rows)
for row in iter(cursor.fetchone, None):
print ('condo_rented_to_others')
print(row)
Upvotes: 1
Views: 1141
Reputation: 599580
You're querying for the literal values 'user_input_fname' and 'user_input_fname'. You need to use parameter substitution to insert the values of the variables instead.
cursor.execute("""SELECT Named_Insured_First_Name, Named_Insured_Last_Name FROM condo_rented_to_others WHERE Named_Insured_First_Name=%s AND Named_Insured_Last_Name=%s """, (user_input_fname, user_input_lname,))
Upvotes: 1