Mysql 检查数据库是否连接到Python
我尝试使用mysql.connector.connect()连接到数据库,我检查了传递给函数的所有参数是否都正常。我希望此代码的输出能够确认与数据库的连接。我在Mysql 检查数据库是否连接到Python,mysql,python,python-3.x,database,connection,Mysql,Python,Python 3.x,Database,Connection,我尝试使用mysql.connector.connect()连接到数据库,我检查了传递给函数的所有参数是否都正常。我希望此代码的输出能够确认与数据库的连接。我在PyCharm中运行这个程序,但什么都没发生,只是在等待。。。MySQL Workbench已经全部设置好了,我的意思是连接已经过测试,工作正常 导入mysql.connector db=mysql.connector.connect(host=“localhost”,user=“root”,password=“Asdf124#”) 如果
PyCharm
中运行这个程序,但什么都没发生,只是在等待。。。MySQL
Workbench已经全部设置好了,我的意思是连接已经过测试,工作正常
导入mysql.connector
db=mysql.connector.connect(host=“localhost”,user=“root”,password=“Asdf124#”)
如果(db):
打印(“连接”)
其他:
打印(“失败”)
根据文档中的示例:
import mysql.connector
from mysql.connector import errorcode
try:
cnx = mysql.connector.connect(user='scott',
database='employ')
except mysql.connector.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print("Something is wrong with your user name or password")
elif err.errno == errorcode.ER_BAD_DB_ERROR:
print("Database does not exist")
else:
print(err)
else:
cnx.close()
这是连接到数据库时处理异常的正确方法。是否回答了您的问题?