无法获得mySQL.connector下载/工作?

2024-10-03 09:16:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将我的raspberry pi连接到一个MySQL数据库,不幸的是我无法将其连接。错误是:

pi@raspberrypi:~ $ python HexCodeImporter2.py
Traceback (most recent call last):
 File "HexCodeImporter2.py", line 1, in <module>
 import mySQL.connector
ImportError: No module named mySQL.connector

pi上用于将数据发送到服务器的代码如下:

^{pr2}$

我试图下载mySQL.connector但运气不好。我怎么能这么做?在


Tags: py数据库mostconnector错误mysqlpicall
2条回答

我不知道python,但我突然想到的是,你没有调用正确的变量名。在

import mySQL.connector

可能是:

import con,因为“con”是保存mysql连接细节的变量。在

如果不是pdo,您还应该从旧的标准mysql迁移到至少mysqli(改进的)

我相信从Python连接到MySQL数据库的首选方法是MySQLdb

如果MySQLdb没有安装,不用担心,这是一个快速安装。在

任何按以下方式编写代码

import MySQLdb

db = MySQLdb.connect(host="localhost", # your host, usually localhost
                     user="username", # your username
                     passwd="password", # your password
                     db="database_name") # name of the data base

cur = db.cursor() 

cur.execute("SELECT * FROM YOUR_TABLE_NAME")

cur.close()  # close the cursor

db.close ()   # close the connection

相关问题 更多 >