我在使用 Python/MySQL 处理日期时间格式时遇到了一些问题。
我使用以下脚本(由 Python 字典提供)计算日期时间:
tempDate = str(eachday.get("date").get("year")).zfill(4) + "-" +
str(eachday.get("date").get("month")).zfill(2) + "-" +
str(eachday.get("date").get("day")).zfill(2) + " " +
str(eachday.get("date").get("hour")).zfill(2) + ":" +
str(eachday.get("date").get("min")).zfill(2) + ":" +
str(eachday.get("date").get("sec")).zfill(2)
产生的值类似于 2012-04-02 04:04:23
。
我可以毫无问题地插入 MySQL。
sql.execute("""INSERT INTO `db`.`table`(`id`, `fk_id`, `time`, `field1`, `field2`) VALUES (NULL, %s, %s, %s, %s);""", (fk_value, tempDate, value1, value2))
DB_CONN.commit()
但是当我尝试删除那个日期时间的任何内容时,
sql.execute("""DELETE FROM `db`.`table` WHERE `time` = "%s";""", (tempDate))
DB_CONN.commit()
它返回有关不正确的日期时间值的警告:
Warning: Incorrect datetime value: ''2012-07-17 23:00:00'' for column 'time' at row 1
如何在 Python 中按日期时间删除?这尤其令人困惑,因为插入相同的变量(没有改变)工作得很好。
最佳答案
您不能在查询字符串中包含 "%s"
中的引号; mysqldb 模块为您添加它们(与在您的 INSERT
中所做的相同,您不尝试在值周围加上引号...)
关于python - 不正确的日期时间值 : '' 2012-07-14 23:00:0 0'' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11387510/