社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

python数据库结果不能用于if语句

Steve8428 • 5 年前 • 1342 次点击  

我的数据从数据库中打印出来时是这样的。

[(0,), (0,), (0,), (0,), (0,), (0,)]

下面是我的python结果脚本

def readDbHeating():

   cursor = mariadb_connection.cursor()
   result = []
   try:
       cursor.execute("SELECT control FROM house_DB")
       for reading in cursor.fetchall():
           result.append(reading)
   except () as e:
       print(e)
   return (result)

while 1:
   result = readDbHeating()
   print result

   waterState = result[0]
   denState = result[3]
   niamhState = result[4]
   downWCstate = result[5]

   if(waterState == 0):
      print("here")
      flag = 0
      print("Water state = %s ") % waterState

我尝试将0设为“0”,并尝试将waterState的传入结果转换为整数,但得到的错误是这是一个元组

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/55864
 
1342 次点击  
文章 [ 2 ]  |  最新文章 5 年前
Alec
Reply   •   1 楼
Alec    5 年前

既然数据是“0”,而不是“0”,你有没有看到它是否有效?

Peritract
Reply   •   2 楼
Peritract    5 年前

waterState 从来没有 0 :它总是一个元组(圆括号中的一组值)。

当你得到你的数据时 [(0,), (0,), (0,), (0,), (0,), (0,)] 把它存起来 results

waterstate = results[0] 把价值 (0,) 进入变量 waterstate ;这是一个元组,永远不会是单个整数。

waterstate = results[0][0] .

这将在列表的第一个位置选择元组的第一个值:

--> --> .