python - 尝试连接外部 MySQL 数据库时卡在连接器上。连接

标签 python mysql raspberry-pi wamp

我正在尝试用树莓派在我的电脑上建立的 wamp 数据库上插入一行。两个设备都连接到同一个路由器,我已经为 RPi 设置了用户权限,但是当我尝试连接到数据库时,代码粘在连接器上。连接功能并且没有捕获任何异常。 这是使用的代码:

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') 行永远不会被调用,并且进程永远保持事件状态。

您知道什么可能会引发这种行为以及如何解决它吗?

最佳答案

尝试使用:

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

关于python - 尝试连接外部 MySQL 数据库时卡在连接器上。连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50552234/

相关文章:

python - 如何使用kivy显示图像

python - 更改 pandastable 中显示的数据框(tkinter 小部件)

MYSQL,在定义表时,UNIQUE 约束是否必须与 INTEGER 结合使用,还是可以是任何数据类型?

python - 在 Raspberry 上使用 Python 进行传感器监控

python - 使用无缓冲 shell 重定向时,stdout 和 stderr 不会进入文件?

Python "in memory DB style"数据类型

python - 如何使用元类创建一个类属性,该元类仅在类本身未定义时才覆盖?

python - 当只有一个实例有意义时,我应该使用一个类吗?

php - 依赖大量读/写时在 php/mysql/ajax 中应避免什么

php - 为什么我不能第二次使用 PHP 从 mysql 中获取?