gpt4 book ai didi

python - 更新MySQL数据库表中的DATE字段

转载 作者:行者123 更新时间:2023-11-29 12:47:11 25 4
gpt4 key购买 nike

无法更新 MySql 数据库中的 DATE 字段。这是我使用的代码:

def update_date(self):
self.cursor.execute("""SELECT min(date),rec_id FROM mytable1 GROUP BY rec_id;""")
data=self.cursor.fetchall()
for row in data:
open_dt=row[0]
pr_id=row[1]
if open_dt:
open_dt= open_dt.date()
print open_dt,pr_id
self.cursor.execute("""UPDATE mytable2 rec_open_date=%s WHERE rec_id=%d;"""%(open_dt,rec_id))
else:
pass

我已经进行了所有必要的导入,并且数据库连接也正常工作。当我运行代码时,它运行时带有警告:

Warning: Out of range value for column 'rec_open_date' at row 1
self.cursor.execute("""UPDATE mytable2 SET rec_open_date=%s WHERE rec_id=%d;""" %(open_dt,rec_id))

最佳答案

如果您引用documentation您将看到 execute 方法调用单独的数据参数,而不是使用 % 格式化字符串。

尝试:

self.cursor.execute("""UPDATE mytable2 rec_open_date=%s WHERE rec_id=%s""",
(open_dt, rec_id))

你所做的是执行这个

UPDATE mytable2 rec_open_date=2011-02-03 12:34:56 WHERE rec_id=1;

这不是一个有效的语句,但出于某种原因执行了某些操作。此外,使用 % 是一个允许 SQL 注入(inject)攻击的安全缺陷。

关于python - 更新MySQL数据库表中的DATE字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25278297/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com