繁体   English   中英

如何使用 python 连接到 mysql 服务器

[英]how to connect using python to mysql server

这可能是一个简单的,但我无法弄清楚。 我无法连接到我在 mysql 中创建的“clearview”数据库。 请指教。 谢谢!

NameError Traceback (last last call last) in 3 try: ----> 4 connection = mysql.connector.connect(host='localhost', 5 database='clearview',

NameError:名称'mysql'未定义

您是否安装了mysql-connector package? 尝试先使用pip install mysql-connector安装它,然后在运行脚本之前导入 package import mysql.connector

在同一个文件的某处,顶部应该有这样的内容: import mysql.connector... connection = mysql.connector.connect(host='localhost', 5 database='clearview', ...你也应该有the python libraries for mysql installed on your machine. see more -> https://dev.mysql.com/doc/connector-python/en/connector-python-installation.html

你用的是哪个版本的 python???

a)首先检查您是否有import mysql.connector 如果它在那里并且不工作,那么可以按照以下步骤操作。

b) 可能是mysql没有正确安装。通过pip search mysql-connector | grep --color mysql-connector-python pip search mysql-connector | grep --color mysql-connector-python

c)通过安装: pip install mysql-connector-python-rf希望它会有所帮助。

所有意见和建议表示赞赏。 我做了这里建议的一切。 是的,我基本上必须将 mysql.connector 安装到我创建的每个环境中以确保。 我想也许,这就是为什么它表明 mysql 没有定义。 教训>..

作为后续问题......是否存在全局安装每个 Python 扩展,所以我不会在多个环境中重新安装它。 以前从未尝试过,因为我可能正在修补不同的环境,不知道我当前使用的是哪个环境。 :)

多谢你们!!!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM