Commit e764a0cc by mohammed.shibili

receiver try,except

parent 8106105d
......@@ -11,21 +11,26 @@ def on_connect(client, userdata, flags, rc):
print("Connected with result code " + str(rc))
except Exception as e:
print("db connection error", e)
except TypeError:
pass
def on_message(client, userdata, message):
data_from_mqtt = json.loads(message.payload.decode())
connection = sqlite3.connect(db_name)
cursor = connection.cursor()
cursor.execute('''CREATE TABLE IF NOT EXISTS mqtt_data
(id INT,first_name varchar(20),last_name varchar(20))''')
for data in data_from_mqtt:
cursor.execute("INSERT INTO mqtt_data VALUES (?,?,?)", (data["id"], data["first_name"],
data["last_name"]))
cursor.execute("SELECT * FROM mqtt_data")
datas = cursor.fetchall()
for row in datas:
print(row)
try:
data_from_mqtt = json.loads(message.payload.decode())
connection = sqlite3.connect(db_name)
cursor = connection.cursor()
cursor.execute('''CREATE TABLE IF NOT EXISTS mqtt_data
(id INT,first_name varchar(20),last_name varchar(20))''')
for data in data_from_mqtt:
cursor.execute("INSERT INTO mqtt_data VALUES (?,?,?)", (data["id"], data["first_name"],
data["last_name"]))
cursor.execute("SELECT * FROM mqtt_data")
datas = cursor.fetchall()
for row in datas:
print(row)
except TypeError:
pass
client = mqtt.Client()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment