Abc Def Abc Def - 1 year ago 250
Python Question

Python 3.5.2 "takes from 2 to 3 positional arguments but 4 were given"

I'm making a program in python that connects to a mysql database and can access it and manipulate it. I am using PyMySQL in order to do this however I am stumped by this error that keeps occuring in my program. Here is the error:

cursor.execute("SELECT * FROM Bookings WHERE ? = ?", record, recordtype)
TypeError: execute() takes from 2 to 3 positional arguments but 4 were given

But I see no error in my code (may be being a bit confident).

def findbooking():
recordtype = input("Which field do you want to search?: ")
record = input("Enter what you want to search: ")
findbookingsql(record, recordtype)

def findbookingsql(record, recordtype): #Connect to Database and find record specified
conn = sql.connect(server,user, password,database)
cursor = conn.cursor()
cursor.execute("SELECT * FROM Bookings WHERE ? = ?",recordtype,record)
row = cursor.fetchone()
return row

Answer Source

Encapsulate the parameters in a tuple, rather than stand alone arguments:

cursor.execute("SELECT * FROM Bookings WHERE ? = ?", (record, recordtype))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download