mysql-connector-python datetime field prepare error

Asked 2 years ago, Updated 2 years ago, 48 views

In mysql-connector-python,
Attempting to insert only one datetime field
Unable to register with prepare error.

  • mysql-connector-python VERSION
 grep VERSION/usr/local/lib/python 3.8/dist-packages/mysql/connector/version.py
VERSION=(8,0,20,',1)
  • MySQL Table
create table(
  id int primary key auto_increment,
  dt datetime
engine=InnoDB"
  • python
#This will succeed
s1 = "insert into (id, dt) values(%s, %s)"
v1 = (1, datetime.datetime.strptime('2000-01-01', '%Y-%m-%d')))
cursor.execute(s1,v1)


# I will fail-failure.
s2 = "insert into(dt) values(%s)"
v2 = (datetime.datetime.strptime('2000-01-01', '%Y-%m-%d'))
cursor.execute(s2,v2)
  • Error Contents
Traceback (most recent call last):
  File ".../t.py", line 17, in <module>
    cursor.execute(s2,v2)
  File"/usr/local/lib/python 3.8/dist-packages/mysql/connector/cursor_cext.py", line 248, in execute
    prepared=self._cnx.prepare_for_mysql(params)
  File"/usr/local/lib/python 3.8/dist-packages/mysql/connector/connection_cext.py", line 632, prepare_for_mysql
    raise ValueError ("Could not process parameters")
ValueError: Could not process parameters
  • Tentative Response
#prepare quit
s2 = "insert into(dt) values('%s')"%('2000-01-01')
cursor.execute(s2)

python python3 mysql

2022-09-30 19:54

1 Answers

Self-Solving
When there was one parameter, adding a comma was required.

v2=(datetime.datetime.strptime('2000-01-01', '%Y-%m-%d'), )


2022-09-30 19:54

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.