mysql - Fix error "TypeError: 'long' object is not iterable"

mysql - Fix error "TypeError: 'long' object is not iterable"

错误信息:

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()

提前致谢!

由于您的查询 returns 一条记录,因此无需迭代。

所以:

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