我有一个类,其中两个变量设置为某个默认值。我已经定义了一个函数,我在函数中将这些变量声明为全局变量。我得到这个错误,说明变量没有定义。一个变量是counter,它将在每个函数调用时递增,而其他变量必须在每次调用时附加消息。在追加多条消息并且计数器值达到某个阈值后,我想将所有这些消息插入到MySQL数据库中。 但是变量没有附加值。它显示变量未定义。
class Message():
insertStatement= ''
insertCounter = 0
def Save(self, msg)
global insertCounter
global insertStatement
if msg.topic.startswith("topic1/"):
insertStatement += "INSERT INTO mydatabase.table VALUES (" + msg.payload.decode("utf-8") + "," + datetime + ");"
elif msg.topic.startswith("topic2/"):
insertStatement += "INSERT INTO mydatabase.table1 VALUES (" + msg.payload.decode("utf-8") + "," + datetime + ");"
elif msg.topic.startswith("topic3/")
insertStatement += "INSERT INTO mydatabase.table2 VALUES (" +msg.payload.decode("utf-8") + "," + datetime + ");"
elif msg.topic.startswith("messages"):
insertStatement += "INSERT INTO mydatabase.table3 VALUES ('" + msg.topic + "'," + msg.payload.decode("utf-8") + "," + datetime + ");"
else:
return
insertcounter +=1
if(insertCounter > 1000):
for result in cursor.execute(insertStatement, multi=True):
print result.row_count # i changed it to result
答案 0 :(得分:1)
请检查最后一行。它应该是result
而不是reslut
。
答案 1 :(得分:1)
您可能只需要实例变量:
class Message():
def __init__(self):
self.insertStatement= ''
self.insertCounter = 0
def Save(self, msg)
if msg.topic.startswith("topic1/"):
self.insertStatement += "INSERT INTO mydatabase.table VALUES (" + msg.payload.decode("utf-8") + "," + datetime + ");"
elif msg.topic.startswith("topic2/"):
self.insertStatement += "INSERT INTO mydatabase.table1 VALUES (" + msg.payload.decode("utf-8") + "," + datetime + ");"
elif msg.topic.startswith("topic3/")
self.insertStatement += "INSERT INTO mydatabase.table2 VALUES (" +msg.payload.decode("utf-8") + "," + datetime + ");"
elif msg.topic.startswith("messages"):
self.insertStatement += "INSERT INTO mydatabase.table3 VALUES ('" + msg.topic + "'," + msg.payload.decode("utf-8") + "," + datetime + ");"
else:
return
self.insertcounter += 1
if self.insertCounter > 1000:
# do the SQL thing
作为一名程序员,你应该尝试非常非常非常很难不使用全局变量。
答案 2 :(得分:1)
您的代码中有几个拼写错误,请更新并检查。
72px × 72px
并递增变量insertCounter
。将其更新为insertcounter
self.insertCounter+1
,而不是result
答案 3 :(得分:1)
变量insertStatement
和insertCounter
是类变量。您不必将它们作为全局内部Save()
引用。要访问这些类变量,请使用Message.insertStatement
和Message.insertCounter
class Message():
insertStatement= ''
insertCounter = 0
def Save(self, msg):
if msg.topic.startswith("topic1/"):
Message.insertStatement += "INSERT INTO mydatabase.table VALUES (" + msg.payload.decode("utf-8") + "," + datetime + ");"
elif msg.topic.startswith("topic2/"):
Message.insertStatement += "INSERT INTO mydatabase.table1 VALUES (" + msg.payload.decode("utf-8") + "," + datetime + ");"
elif msg.topic.startswith("topic3/")
Message.insertStatement += "INSERT INTO mydatabase.table2 VALUES (" +msg.payload.decode("utf-8") + "," + datetime + ");"
elif msg.topic.startswith("messages"):
Message.insertStatement += "INSERT INTO mydatabase.table3 VALUES ('" + msg.topic + "'," + msg.payload.decode("utf-8") + "," + datetime + ");"
else:
return
Message.insertcounter +=1
if(Message.insertCounter > 1000):
for result in cursor.execute(Message.insertStatement, multi=True):
print result.row_count # i changed it to result