MySQL Connector could not process parameters

2495
February 05, 2019, at 11:20 AM

I'm trying to loop through an array and insert each element into a table. As far as I can see my syntax is correct and I took this code straight from Microsoft Azure's documentation.

try:
   conn = mysql.connector.connect(**config)
   print("Connection established")
except mysql.connector.Error as err:
  if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
    print("Something is wrong with the user name or password")
  elif err.errno == errorcode.ER_BAD_DB_ERROR:
    print("Database does not exist")
  else:
    print(err)
else:
  cursor = conn.cursor()
data = ['1','2','3','4','5']

for x in data:
   cursor.execute("INSERT INTO test (serial) VALUES (%s)",(x))
   print("Inserted",cursor.rowcount,"row(s) of data.")
conn.commit()
cursor.close()
conn.close()
print("Done.")

When I run this is gets to cursor.execute(...) and then fails. Here is the stack trace.

Traceback (most recent call last): File "test.py", line 29, in cursor.execute("INSERT INTO test (serial) VALUES (%s)",("test")) File "C:\Users\AlexJ\AppData\Local\Programs\Python\Python37\lib\site-packages\mysql\connector\cursor_cext.py", line 248, in execute prepared = self._cnx.prepare_for_mysql(params) File "C:\Users\AlexJ\AppData\Local\Programs\Python\Python37\lib\site-packages\mysql\connector\connection_cext.py", line 538, in prepare_for_mysql raise ValueError("Could not process parameters") ValueError: Could not process parameters

Answer 1

Try this:

for x in data:
    value = 'test'
    query = "INSERT INTO test (serial) VALUES %s"
    cursor.execute(query,(value,))
    print("Inserted",cursor.rowcount,"row(s) of data.")

Since you are using mysql module, cursor.execute requires a sql query and a tuple as parameters

Rent Charter Buses Company
READ ALSO
My Function is taking far too long and causing the page to timeout

My Function is taking far too long and causing the page to timeout

We have a new magento storeMagento uses phtml files and "blocks" to display data on the front end

141
JSON_CONTAINS string everywhere in the structure

JSON_CONTAINS string everywhere in the structure

I have a JSON column in my MySQL database (57

133
Exclude same values and specific ruleset query

Exclude same values and specific ruleset query

it's been a while since I worked with databases and can't wrap my head around how to do these at the moment, so I would really appreciate it if anyone could lend me a hand with these two questions/queries

184
MySQL query to generate a user list who have at least one order but no order in this month till now

MySQL query to generate a user list who have at least one order but no order in this month till now

I have two table user and orderwhere foreign key in order table is user_id

186