mysql-修复错误“ TypeError:'long'对象不可迭代”

时间:2019-10-05 00:30:30

标签: python mysql flask raspberry-pi mysql-python

错误消息:

TypeError: 'long' object is not iterable

错误消息中指出的代码:

def maxRowsTable():
    for row in x.execute("SELECT COUNT(temp) from sensors"): #this
        maxNumberRows=row[0]
    return maxNumberRows

我的MySQLdb连接:

conn = MySQLdb.connect(
    host="localhost",
    user="user",
    passwd="pass",
    database="data"
)
x = conn.cursor()

提前谢谢!

1 个答案:

答案 0 :(得分:0)

由于查询返回单个记录,因此无需进行迭代。

所以:

x.execute("SELECT COUNT(temp) from sensors")
row = x.fetchone()
maxNumberRows = row[0]