【发布时间】:2019-05-29 22:05:50
【问题描述】:
我在python中有这段代码,它的作用是读取来自串口的数据;数据从一个 XBee 发送,另一个 XBee 接收,第二个 XBee 连接到计算机端口,它接收到的数据是我读取的,然后我尝试将这些数据发送到 mysql 数据库,但我发送以下错误:
"MySQLdb._exceptions.ProgrammingError: %b 需要一个类似字节的对象,或者一个实现 bytes 的对象,而不是 'dict'"
代码:
import serial
from xbee import XBee
import MySQLdb
serial_port = serial.Serial('COM5', 9600)
xbee = XBee(serial_port)
while True:
try:
value = xbee.wait_read_frame()
sql_conn = MySQLdb.connect('localhost', 'root', 'pass', 'XBee')
cursor = sql_conn.cursor()
cursor.execute("INSERT INTO xbeedata (value) VALUES (%s)", (value))
data = cursor.fetchall()
cursor.close()
print (xbee.wait_read_frame())
except KeyboardInterrupt:
break
serial_port.close()
问候。
【问题讨论】:
标签: python mysql python-3.x arduino