我试图通过使用relStack表来跟踪需要完成哪些任务。该脚本将接受一个参数并将其作为字符串放入表中。当我运行脚本时,脚本会吐出一个错误,指出relStack是一个只读数据库。我没有对其施加任何限制,因此它应该可写。代码在这里(很多注释):
#!/usr/bin/python
import sys
import subprocess
import sqlite3
# This allows this script to interact with the "Database" (the relay.db file)
DB = sqlite3.connect("/home/pi/Documents/relay.db")
# Using the cursor will allow the script to execute SQLite commands/statements
csr = DB.cursor()
# Getting the names of all items in the "stack"
c = csr.execute("SELECT Name FROM relStack").fetchall()
# Checks if the script is being imported or ran as a program
if __name__ == "__main__":
# sys.argv[1] stands for the first argument/parameter that is entered when
# the script is called
a = sys.argv[1]
# Checking if the stack is empty. If so, then the execution script is called
# because it would've finished running and we will have to call it again in
# order for it to work
if (str(c) == "[]"):
# This adds a "request" into the stack, so the execution script knows
# which relay to toggle
addReq = csr.execute("INSERT INTO relStack VALUES(\"" + str(a) + "\")")
# Calling the execution script
subprocess.Popen("sudo python /home/pi/Documents/trigger.py", shell = True)
else:
# Adding "request" to stack
addReq = csr.execute("INSERT INTO relStack VALUES(\"" + str(a) + "\")")
乍一看似乎没有什么不妥。我想念什么吗?