

mysql-connector-python で、  
datetime フィールド１つだけinsertしようとすると  
prepareエラーで登録できません。

  * mysql-connector-python VERSION

[code]

    grep VERSION /usr/local/lib/python3.8/dist-packages/mysql/connector/version.py
    VERSION = (8, 0, 20, '', 1)
    
[/code]

  * MySQLテーブル

[code]

    create table t(
      id int primary key auto_increment,
      dt datetime
    ) engine = InnoDB"
    
[/code]

  * python

[code]

    # これは成功する
    s1 = "insert into t(id, dt) values (%s, %s)"
    v1 = (1, datetime.datetime.strptime('2000-01-01', "%Y-%m-%d"))
    cursor.execute(s1, v1)
    
    
    # こっちは失敗する
    s2 = "insert into t(dt) values (%s)"
    v2 = (datetime.datetime.strptime('2000-01-01', "%Y-%m-%d"))
    cursor.execute(s2, v2)
    
[/code]

  * エラー内容

[code]

    Traceback (most recent call last):
      File ".../t.py", line 17, in <module>
        cursor.execute(s2, v2)
      File "/usr/local/lib/python3.8/dist-packages/mysql/connector/cursor_cext.py", line 248, in execute
        prepared = self._cnx.prepare_for_mysql(params)
      File "/usr/local/lib/python3.8/dist-packages/mysql/connector/connection_cext.py", line 632, in prepare_for_mysql
        raise ValueError("Could not process parameters")
    ValueError: Could not process parameters
    
[/code]

  * 暫定対応

[code]

    # prepare辞める
    s2 = "insert into t(dt) values ('%s')" % ('2000-01-01')
    cursor.execute(s2)
    
[/code]


CC BY-SA 4.0
2020-06-15T13:30:31.757
67692
2020-06-20T23:27:34.853
26568
question
python python3 mysql
mysql-connector-python の datetime フィールドprepareエラー


自己解決  
パラメータが１個の時、カンマの追加が必須でした。

[code]

    v2 = (datetime.datetime.strptime('2000-01-01', "%Y-%m-%d"),)
    
[/code]


CC BY-SA 4.0
2020-06-20T23:27:34.853
67848
2020-06-20T23:27:34.853
26568
67692
answer