簡體   English   中英

Raspberry Pi上的Python腳本在mysql語句執行時出錯

[英]Python Script on Raspberry Pi gets error on mysql statement execution

這是錯誤:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM