我正在用 python 编写一个程序,该程序连接到 mysql 数据库并可以访问和操作它。我正在使用 PyMySQL 来执行此操作,但是我对程序中不断出现的错误感到困惑。这是错误:
cursor.execute("SELECT * FROM Bookings WHERE ? = ?", record, recordtype)
TypeError: execute() takes from 2 to 3 positional arguments but 4 were given
但我在我的代码中没有看到任何错误(可能有点自信)。
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()
conn.close()
return row
最佳答案
将参数封装在一个元组中,而不是独立的参数:
cursor.execute("SELECT * FROM Bookings WHERE ? = ?", (record, recordtype))
关于 python 3.5.2 "takes from 2 to 3 positional arguments but 4 were given",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42096517/