我第一次在树莓派上玩python.
我有一个查询SQL表并返回设置值的脚本.
我无法工作的是从结果中设置python变量.
这是我的代码的一部分
# execute SQL query using execute() method.
cursor.execute("select id from wallboard")
# Fetch a single row using fetchone() method.
data = cursor.fetchone()
# disconnect from server
db.close()
result = str("%s " % data)
print result
if result == 1:
Run Code Online (Sandbox Code Playgroud)
打印显示结果没问题,但它没有进入if语句.
我是python的新手,所以它可能是一个简单的修复,但我很难过.
谢谢
不要将结果转换为字符串:
>>> "1" == 1
False
Run Code Online (Sandbox Code Playgroud)
另请注意,fetchone()会返回一行结果,表示为元组 - 获取第一项获取实际id列值:
result = cursor.fetchone()[0]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2760 次 |
| 最近记录: |