pjp
pjp

Reputation: 93

Using python for an INSERT with MySQL

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!

Upvotes: 3

Views: 707

Answers (1)

warvariuc
warvariuc

Reputation: 59684

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 %.

Upvotes: 3

Related Questions