Fader910
Fader910

Reputation: 71

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()
    conn.close()
    return row

Upvotes: 2

Views: 9399

Answers (2)

MSeifert
MSeifert

Reputation: 152795

sqlite3.Connection.execute (and also for similar methods on other Connection classes) take up to 3 parameters:

  • The first is self, you call it on an instance: cursor so that one is passed implicitly and automatically.

  • The second is sql that seems correct.

  • The third is optional parameters that's where it goes wrong. What is your record and recordtype? Did you mean to pass them in as sequence?

    cursor.execute("SELECT * FROM Bookings WHERE ? = ?", 
                   [recordtype, record])
    

Upvotes: 1

Uriel
Uriel

Reputation: 16224

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

cursor.execute("SELECT * FROM Bookings WHERE ? = ?", (record, recordtype))

Upvotes: 7

Related Questions