[英]Unable to insert a zulu date into mysql database using flask_mysqldb
When I run the following iwth the value of Job_date being "2023-01-02T21:00:00.504Z"当我运行以下命令时,Job_date 的值为“2023-01-02T21:00:00.504Z”
@api.route('/addjob', methods=["POST"])
def addjob():
f = open('addjob.txt','w')
f.write(str(request.json))
cursor = mysql.connection.cursor()
sql = '''INSERT into jobs (job_date,cust_auto_id,cost,paid,mileage,job_desc,person_id) values (str_to_date(%s,"%%Y-%%m-%%dT%%H:%%i:%%sZ"),%s,%s,%s,%s,%s,%s)'''
try:
cursor.execute(sql,(request.json.get("jobdate", None),request.json.get("whichauto", None),request.json.get("jobcost", None),request.json.get("paystatus", None),request.json.get("mileage", None),request.json.get("jobdesc", None),request.json.get("whichperson", None)))
mysql.connection.commit()
except Exception as e:
f.write(str(e))
return jsonify([])
I get我得到
"Incorrect datetime value: '2023-01-02T21:00:00.504Z' for function str_to_date
In mysql 8, you can specify an %f format to accept up to 6 digits of partial second:在 mysql 8 中,您可以指定一个 %f 格式来接受最多 6 位的部分秒:
str_to_date(%s,"%%Y-%%m-%%dT%%H:%%i:%%s.%%fZ")
In earlier versions or mariadb, you can simply remove the Z instead of using str_to_date:在早期版本或 mariadb 中,您可以简单地删除 Z 而不是使用 str_to_date:
replace(%s,"Z","")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.