qq_36524174 2016-11-22 06:14 采纳率: 0%
浏览 1353

关于Python读取数据库问题请教

我使用while 1循环读取数据库的一个表内容,当内容改变时,无法实时改变,求大神帮我看下代码
import MySQLdb
import serial
import time

ser = serial.Serial('/dev/ttyACM0',9600,timeout=1)
conn=MySQLdb.connect(host="192.168.191.5",user="root",passwd="123456",db="test")
cursor = conn.cursor()
try:
while 1:

    time.sleep(1.5)
    n = cursor.execute("select state from t_led")
    r = cursor.fetchone()
    p = r[0]
    ser.write(p)
    a = ser.readline()
    print r

except KeyboardInterrupt:
conn.close()
ser.close()
cursor.close()

  • 写回答

4条回答

  • 菜鸟biubiu 2016-11-22 07:10
    关注

    在conn=MySQLdb.connect(host="192.168.191.5",user="root",passwd="123456",db="test")后面之行conn的autocommit(True).
    conn=MySQLdb.connect(host="192.168.191.5",user="root",passwd="123456",db="test")
    conn.autocommit(True)

    评论

报告相同问题?