Reputation: 41
I am trying to insert some values from python to MySQL and I receive the following error.
Connection and code in Python:
conn = MySQLdb.connect(host="localhost",user="lpr",passwd="B1ack53@",db="lpr")
c=conn.cursor()
c.execute("INSERT INTO liccaptured(realtime, proctime, plate, confid, uuid) VALUES (%s,%s,%s,%s,%s)", realtime,proctime,plate,confid,uuid)
Error message:
c.execute("INSERT INTO liccaptured(realtime, proctime, plate, confid, uuid) VALUES (%s,%s,%s,%s,%s)", realtime,proctime,plate,confid,uuid)
TypeError: execute() takes at most 3 arguments (7 given)
I tried looking for similar errors and could not figure out what I am doing wrong. Any help is appreciated thanks.
Upvotes: 1
Views: 162
Reputation: 107567
You did not correctly pass the tuple string operator. Consequently, c.execute()
assumes multiple arguments. Simply, place %
just after string with no commas, and wrap all variables in parentheses
c.execute("INSERT INTO liccaptured (realtime, proctime, plate, confid, uuid) \
VALUES (%s,%s,%s,%s,%s)" % (realtime,proctime,plate,confid,uuid))
Alternatively, consider using string format:
c.execute("INSERT INTO liccaptured (realtime, proctime, plate, confid, uuid) \
VALUES ({0},{1},{2},{3},{4})".format(realtime,proctime,plate,confid,uuid))
Upvotes: 0
Reputation: 25381
Error message says everything: the function execute
has at most 3 arguments, but you're calling it with 7 arguments. So you just need to call it properly with 3 arguments.
According to documentation, execute
function's syntax is as follows:
cursor.execute(operation, params=None, multi=False)
operation
is SQL query as stringparams
is an array of parametersmulti
is boolean flagUpvotes: 0
Reputation: 5997
The error you are running into comes from the fact that you are treating the cursor.execute
method as one which accepts a variable number of arguments:
c.execute(operation, arg1, arg2, ..., argn) # won't work
execute
only accepts a fixed number of arguments though. The paramaters to the SQL statement itself are passed in as a single argument that is a tuple:
my_args = (arg1, arg2, ..., argn)
c.execute(operation, my_args) # this will work
c.execute(operation, (arg1, arg2, ..., argn)) # just the same as above
Upvotes: 1