gpt4 book ai didi

python - 如何使用日期时间检索行(python 和 mysql)

转载 作者:行者123 更新时间:2023-11-30 00:10:38 26 4
gpt4 key购买 nike

这是我的代码:

now  = datetime.datetime.now().replace(microsecond=0)
curs.execute("SELECT name, msgDate, FROM test where msgDate=%s",(now))

我收到这些消息:

File "C:\Python\lib\site-packages\mysql\connector\cursor.py", line 220, in _process_params
res = list(map(to_mysql,res))
TypeError: 'datetime.datetime' object is not iterable
During handling of the above exception, another exception occurred:

Traceback (most recent call last):

File "C:\projectse\Email\src\a.py", line 65, in <module>
curs.execute("SELECT name, msgDate FROM messages where msgDate=%s",(now))
File "C:\Python\lib\site-packages\mysql\connector\cursor.py", line 300, in execute
stmt = operation % self._process_params(params)
File "C:\Python\lib\site-packages\mysql\connector\cursor.py", line 225, in _process_params
"Failed processing format-parameters; %s" % e)
mysql.connector.errors.ProgrammingError: -1: Failed processing format-parameters;
'datetime.datetime' object is not iterable

有什么建议吗?

最佳答案

我认为你应该替换:

curs.execute("SELECT name, msgDate, FROM test where msgDate=%s",(now))

与:

curs.execute("SELECT name, msgDate, FROM test where msgDate=%s",%(now))

并将其用作您的时间变量:

import time
now=time.strftime('%Y-%m-%d %H:%M:%S')

如果您好奇为什么会出现问题,请尝试查看 now = str(datetime.datetime.now().replace(microsecond=0)) 在 Python 解释器中的样子。

关于python - 如何使用日期时间检索行(python 和 mysql),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24100991/

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