使用 Python 的 MQTT 和 SQLite3 通信

MQTT and SQLite3 communication using Python

我正在开发与 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 列。