3

I am trying to insert into a MYSQL database with the following commands:

add_contact = "INSERT INTO contacts (id, name, industry, phone, fax, url, pobox, emirate,ranking) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"

data_contact = (0, fields[2], fields[0], fields[5], fields[6], fields[1], fields[3],  fields[4], float(totalhits))

cursor.execute(add_contact, data_contact)

and I am getting the following error:

Traceback (most recent call last):
  File "reputation.py", line 53, in <module>
    cursor.execute(add_contact, data_contact)
  File "/Library/Python/2.7/site-packages/mysql/connector/cursor.py", line 381, in execute
    "Wrong number of arguments during string formatting")
mysql.connector.errors.ProgrammingError: Wrong number of arguments during string formatting

Which is making me doubt my ability to count to 9 :-)

What can possibly be wrong with this code?

UPDATE:

Changing to

add_contact = ("INSERT INTO contacts (id, name, industry, phone, fax, url, pobox, emirate,ranking)" "VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s)")

Plus

cnx.commit()

Solved it!

Thanks!

1 Answer 1

3

I think you should use % instead of ?:

add_contact = "INSERT INTO contacts (id, name, industry, phone, fax, url, pobox, emirate,ranking) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)"

? is used as placeholder in sqlite3. In psycopg2 and MySQLdb it's %.

Sign up to request clarification or add additional context in comments.

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.