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

时间:2016-04-07 04:00:58

标签: python mysql raspberry-pi

这是错误:

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行)

1 个答案:

答案 0 :(得分:0)

第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