将时间戳转换为mysql格式

时间:2020-01-30 15:05:50

标签: python-3.5 mariadb-10.4

我有一个格式为“ 2020-01-30T14:55:52.373959Z”的日期时间,并且我正在尝试插入一个出现以下错误的sql数据库;

mysql.connector.errors.DataError: 1292 (22007): Incorrect datetime value: '2020-01-30T14:55:06Z' for column `edb`.`flight_path`.`time_stamp` at row 1

所以我尝试用以下内容转换日期;

import time
import datetime
json_time = value.get('gatewayTimestamp', time.time())
print(json_time)
time_stamp = datetime.datetime.fromtimestamp(json_time).strftime('Y%-%m-%d %H:%M:%S')
sql = "insert into flight_path values (%s)"
cursor.execute(sql, (time_stamp)

出现此错误:

2020-01-30T14:55:52.373959Z
Traceback (most recent call last):
  File "eddn_journal_listener.py", line 308, in <module>
    main()
  File "eddn_journal_listener.py", line 51, in main
    time_stamp = datetime.datetime.fromtimestamp(json_time).strftime('Y%-%m-%d %H:%M:%S')
TypeError: a float is required

0 个答案:

没有答案