gpt4 book ai didi

Raspberry Pi 上的 Python 脚本在执行 mysql 语句时出现错误

转载 作者:行者123 更新时间:2023-11-29 21:10:02 26 4
gpt4 key购买 nike

这是错误:

Traceback (most recent call last):
File "scheduler.py", line 27, in <module>
cur.execute(sql)
File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 169, in execute
self.errorhandler(self, TypeError, m)
File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
TypeError: must be string or read-only buffer, not tuple

这是文件中的代码:

<小时/>
# Get Current Weekday/Time/datetime
today = datetime.datetime.today().weekday()
current = datetime.datetime.today()
currentTime = datetime.datetime.now().time()

# Get Schedule Entries From Database
sql = ("""SELECT (start, stop, interrupt) FROM schedule WHERE day=%s""",(today))
cur.execute(sql)

# Use Schedule times to set / check status of pump / heater
for (start, stop, interrupt) in cur:
if interrupt == 0 and start < currentTime and stop > currentTime:
cur.execute("""INSERT INTO status (datetime, pump, heater) VALUES (%s,%s,%s)""",(current, 1, 1))
elif interrupt == 1 and start > currentTime or stop < currentTime:
cur.execute("""UPDATE schedule SET interrupt=%s WHERE day=%s""",(0,today))

它说错误发生在第 36 行,但该行是注释(上面显示的最后一行代码是第 34 行)

最佳答案

第36行是实际mysql库中的错误位置。您的错误位于第 27 行:

File "scheduler.py", line 27, in <module>
cur.execute(sql)

cur.execute 抛出错误,因为您向其传递了一个元组(有序对)。线路

sql = ("""SELECT (start, stop, interrupt) FROM schedule WHERE day=%s""",(today))

没有按照你的想法去做。它从字符串和时间创建一个元组,而不是格式化字符串。我想你想要的是这样的:

sql = """SELECT (start, stop, interrupt) FROM schedule WHERE day=%s""" % today

关于Raspberry Pi 上的 Python 脚本在执行 mysql 语句时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36466470/

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