gpt4 book ai didi

python - 使用 Python 的 MQTT 和 SQLite3 通信

转载 作者:行者123 更新时间:2023-12-05 04:32:18 25 4
gpt4 key购买 nike

我正在开发一个与 SQLite 数据库通信的 MQTT 应用程序(我正在使用 python 和 SQLite3)首先,我创建了我的数据库,你会在下面找到相应的 python 代码。然后编写发布者和订阅者脚本。问题是,在执行订阅者脚本以获取用户名时,我得到了这个输出:

Client created successfully ! 
Client connected
The default result code for this connection is: 0
THIS IS THE ON_MESSAGE FUNCTION: Ok
The received ID is:
b'1004'
the name is:
('User2',)

而预期的输出是

Client created successfully ! 
Client connected
The default result code for this connection is: 0
THIS IS THE ON_MESSAGE FUNCTION: Ok
The received ID is:
'1004'
the name is:
'User2'

有人能告诉我如何解决这个问题吗?如果您需要更多信息,请告诉我

提前谢谢你。

这是订阅者的Python代码

import paho.mqtt.client as mqtt
import time
import sqlite3

port = 1883
brocker = "localhost"
topic = "Auth"

def on_connect(client, userdata, flags, rc):

print("Client connected \n")
print("The default result code for this connection is: "+str(rc))

def on_message(client, userdata, message):

print("THIS IS THE ON_MESSAGE FUNCTION: Ok")
time.sleep(1)
print("The received ID is: ")
print(message.payload)

conn = sqlite3.connect('EMP_DB')
cur = conn.cursor()
cur.execute('''SELECT emp_name FROM employee WHERE emp_id = 1004''')
print("the name is: ")
res = cur.fetchone()
print(res)
conn.commit()
conn.close()
if message.retain==1:
print("This is a retained message \n")
try:

clt = mqtt.Client()
print(" Client created successfully ! \n")

clt.on_connect = on_connect
clt.on_message = on_message

clt.connect(brocker,port)
clt.loop_start()
clt.subscribe(topic)
time.sleep(4)
print("subscribtion: successful \n")
clt.loop_stop()

except Exception as inst:
print("\n Exception found \n")
print(type(inst))
print(inst.args)
print(inst)
print("\n")

这是发布者的 Python 代码

import paho.mqtt.client as mqtt
import time

port = 1883
brocker = "localhost"
message = 1004
topic = "Auth"

def on_publish(client,userdata,mid):
print("on_publish callback mid: "+str(mid))
print("The message published is: " +str(message))

def on_connect(client, userdata, flags, rc):
print("The default result code for this connection is: "+str(rc))

def on_disconnect(client,userdata,rc):
print("client disconnected \n")

def main():
try:
clt = mqtt.Client("client1")
print("Client CREATED successfully \n")

clt.on_connect = on_connect
clt.on_publish = on_publish

clt.connect(brocker,port)
print("Client connected \n")

ret = clt.publish(topic,message)
time.sleep(4)
print("The publish result is : "+str(ret)+"\n")

clt.on_disconnect = on_disconnect
clt.disconnect()

except Exception as inst:
print("\n Exception found \n")
print(type(inst))
print(inst.args)
print(inst)
print("\n")

main()

最佳答案

传入的消息有效负载将始终是一个字节数组(打印时由前导 b' 指示),如果您想将其转换为字符串,您应该使用:

message.payload.decode("utf-8")

至于打印出结果,你有一个 Row对象,您将需要使用

res['emp_name']

从行中提取 emp_name 列。

关于python - 使用 Python 的 MQTT 和 SQLite3 通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71690550/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com