1

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.

0

3 Answers 3

1

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
Sign up to request clarification or add additional context in comments.

Comments

0

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 string
  • params is an array of parameters
  • multi is boolean flag

Comments

0

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))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.