MySQL的日期值格式
我的计划是将日期(day_of_test)插入MySQL数据库中,其中DataFormat被称为日期。
我必须以您在下面看到的格式保持语法。但是不幸的是,我尝试将日期专门存储在Sytax值(%S ...)中的尝试似乎不起作用。
有人知道成功地插入Day_of_test的最简单方法吗?
非常感谢
import mysql.connector
mydb = mysql.connector.connect(host="34.xxx.xxx.xxx", user="xxxx", password="xxxxx", database='btc-analysis-db')
c = mydb.cursor()
btc_est = 150.2
sap_close = 100.23
gold_close = 120.2
bonds_close = 210.12
btc_actual = 130.12
day_of_test = "2022-04-20"
c = mydb.cursor()
c.execute(
"INSERT INTO `ML1`(`day_of_test`, `btc_est`, `sap_close`, `bonds_close`, `gold_close`, `btc_actual`) VALUES (%s, %d, %d, %d, %d, %d);" % (
day_of_test, btc_est, sap_close, bonds_close, gold_close, btc_actual))
mydb.commit()
c.execute("select * from ML1")
result = c.fetchall()
my plan is to insert the date (day_of_test) into a MySql database, where the dataformat is referenced as a Date.
I have to keep the syntax in the format you see below. But unfortunately my attempts to store specifically the date as a string within the sytax VALUES(%s...) don't seem to work.
Does anyone know the easiest way to adjust my syntax to insert day_of_test successfully?
Thank you all very much
import mysql.connector
mydb = mysql.connector.connect(host="34.xxx.xxx.xxx", user="xxxx", password="xxxxx", database='btc-analysis-db')
c = mydb.cursor()
btc_est = 150.2
sap_close = 100.23
gold_close = 120.2
bonds_close = 210.12
btc_actual = 130.12
day_of_test = "2022-04-20"
c = mydb.cursor()
c.execute(
"INSERT INTO `ML1`(`day_of_test`, `btc_est`, `sap_close`, `bonds_close`, `gold_close`, `btc_actual`) VALUES (%s, %d, %d, %d, %d, %d);" % (
day_of_test, btc_est, sap_close, bonds_close, gold_close, btc_actual))
mydb.commit()
c.execute("select * from ML1")
result = c.fetchall()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用的是Python字符串格式化,那么您会在日期的值周围丢失引号,而您的sintax应该是:
但是您不应该这样做。您应该使用已准备好的陈述(以避免SQL注入其他问题),您的sintax应该这样:
If you are using python string formating, then you are missing quotation marqs around the value for date, and your sintax should be:
But you shouldn´t do it that way. you should use prepared statements (to avoid SQL injection an other problems), and your sintax should be like this: