1

I've looked at previous questions but the solutions suggested never worked. What I have found is that if I create a new column in the table and save 2 variables it works as intended. I have a working insert into a different table earlier in the code so I know the method works. The only difference being this query has a single variable.

Here is the code:

cursor.execute("INSERT INTO docs (filingNum) VALUES (%s)", (docNum))
database.commit()

I get the following output. The string of numbers being what I want to insert into the table.

docNum is: 0000928052-21-000002
Traceback (most recent call last):
  File "/home/andy/Documents/testing/concat.py", line 76, in <module>
    sec_rss()
  File "/home/andy/Documents/testing/concat.py", line 68, in sec_rss
    cursor.execute("INSERT INTO docs (filingNum) VALUES (%s", (docNum))
  File "/home/andy/.local/lib/python3.9/site-packages/mysql/connector/cursor_cext.py", line 257, in execute
    prepared = self._cnx.prepare_for_mysql(params)
  File "/home/andy/.local/lib/python3.9/site-packages/mysql/connector/connection_cext.py", line 651, in prepare_for_mysql
    raise ValueError("Could not process parameters")
ValueError: Could not process parameters
  • Put a comma for `docNum` so that it's properly treated as a tuple: `(docNum,)`. That gets passed in correctly as parameter of the query. Otherwise, it's just a single string and not a tuple of parameter values/list of param value. So the whole thing would be: `cursor.execute("INSERT INTO docs (filingNum) VALUES (%s)", (docNum,))` – aneroid Jun 16 '21 at 15:46
  • Related solution/dupe: [How to create a tuple with only one element](https://stackoverflow.com/questions/12876177/how-to-create-a-tuple-with-only-one-element). Still, +1 for a well-asked first question. Welcome to StackOverflow! – aneroid Jun 16 '21 at 15:50
  • 1
    @aneroid. Many thanks for the super quick reply the comma fixed my issue. I really appreciate it. – Andrew Longstaff Jun 16 '21 at 17:12

1 Answers1

-1

Does the statement:

cursor.execute("INSERT INTO docs (filingNum) VALUES (%s", (docNum))

require a closing paren?

cursor.execute("INSERT INTO docs (filingNum) VALUES (%s)", (docNum))

Typically I use a statement something like:

cursor.execute('''INSERT INTO [docs] values (?)''', docNum)

  • Please format the code properly using block quotes \`\`\`like this\`\`\`. This is more of a comment than an answer. And what do you mean _"Typically I use a statement something like (**i.e. untested**) "_ - you either use it or you don't. If you do use it, then it's not untested. – aneroid Jun 16 '21 at 15:57
  • I have used similar expressions in the past. I have not used that explicit statement, I will edit – MrMxyzptlk20 Jun 16 '21 at 15:58
  • 1
    The use of `%s`, `?` etc as values placeholders is governed by the value of the DB API connector's `paramstyle` attribute. – snakecharmerb Jun 16 '21 at 16:13