Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 检查数据库是否连接到Python_Mysql_Python_Python 3.x_Database_Connection - Fatal编程技术网

Mysql 检查数据库是否连接到Python

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#”) 如果

我尝试使用mysql.connector.connect()连接到数据库,我检查了传递给函数的所有参数是否都正常。我希望此代码的输出能够确认与数据库的连接。我在
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()
这是连接到数据库时处理异常的正确方法。

是否回答了您的问题?