尝试连接外部mysql数据库时卡在connector.connect上

dphi5xsq  于 2021-06-23  发布在  Mysql
关注(0)|答案(2)|浏览(338)

我试图在我的电脑上建立的wamp数据库中插入一行带有raspberry pi的数据。两个设备都连接到同一路由器,我为rpi设置了一个用户权限,但当我尝试连接到数据库时,代码仍保留在connector.connect函数中,没有捕获任何异常。这是使用的代码:

from mysql import connector

print('0')
try:
    con = connector.Connect(user='own_pi',password='password',database='tempbase',host='192.168.0.104', port=3306)
except connector.Error as e:
    print("Error code:", e.errno)        # error number
    print("SQLSTATE value:", e.sqlstate) # SQLSTATE value
    print("Error message:", e.msg)       # error message
    print("Error:", e)                   # errno, sqlstate, msg values
    s = str(e)
    print("Error:", s)                   # errno, sqlstate, msg values

print('1')
cur = con.cursor()
print('2')
cur.execute("INSERT INTO `sensor_readings` (`uid`, `local_id`, `type`, `date`, `reading`) VALUES ('7', '4', 'temperature', '2018-06-04', '24.4');")
print('3')
con.commit()
print('4')
con.close()
print('5')

永远不会调用此print('1')行,并且进程将永远处于活动状态。
你知道是什么引起了这样的行为吗?我怎样才能纠正它?

5tmbdcev

5tmbdcev1#

尝试使用:

from mysql import connector

print('0')
try:
    con = connector.Connect(user='own_pi',password='password',database='tempbase',host='192.168.0.104', port=3306)
    print('1')

    cur = con.cursor()
    print('2')
    cur.execute("INSERT INTO `sensor_readings` (`uid`, `local_id`, `type`, `date`, `reading`) VALUES ('7', '4', 'temperature', '2018-06-04', '24.4');")
    print('3')
    con.commit()
    print('4')
    con.close()
    print('5')

except connector.Error as e:
    print("Error code:", e.errno)        # error number
    print("SQLSTATE value:", e.sqlstate) # SQLSTATE value
    print("Error message:", e.msg)       # error message
    print("Error:", e)                   # errno, sqlstate, msg values
    s = str(e)
    print("Error:", s)                   # errno, sqlstate, msg values
pxyaymoc

pxyaymoc2#

问题的根本原因是Windows10默认防火墙设置。谢谢你的支持。

相关问题