pythonからmysqlのデータ入力に関する質問です。
使用環境
ubuntu 20.02 LTS
python 3.10
mysql 8.0.29(ubuntuの中でdockerで作成)
状況
1 ubuntu から python mysql.connectorでsqlに接続、データを取得
cur.execute("SELECT Time `**_log` WHERE 1";) log = cur.fetchall()
入手した**logは
(id, datetime.datetime(2022, 7, 3, 0, 1, 43), 'IP_address')
となっています。
2 python pandasでデータ処理
new = pd.read_csv(**_log, engine='python', header=None)
で読み込むと、カラムのデータ型が、「datatime.datatime」から「datetime64[ns]」に勝手に変わる。
その後、pandasでデータ処理
3 再度、ubuntu からsqlに処理済みのデータをインサート
list = new2.to_numpy().tolist() cursor.executemany("INSERT INTO **_test (Time,IP) VALUES (%s, %s)", list) conn.commit()
を行うと、
mysql.connector.errors.ProgrammingError: Failed processing format-parameters; Python 'timestamp' cannot be converted to a MySQL type
が出て、インサートできません。
テーブルのTimeの型は、datetimeにしており、他のテーブルとの整合性もあり、timestampにはしたくありません。
色々と調べたところ、「datetime64[ns]」は'timestamp'として認識されるようなのですが、これを'datetime'に戻す方法がわかりません。
ほかに、df.to_sqlを用いる方法もあるようですが、私の環境では接続できませんでした。
何か良い方法はありませんか?

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/07/04 07:32