0

I try to connect to database using mysql.connector.connect(), I checked if all arguments passed to function are ok and they are. I want the output of this code to confirm the connection made to database. I run this in PyCharm and nothing happens, just waiting for something... MySQL Workbench is all set up, I mean the connection is tested and it works well.

import mysql.connector

db = mysql.connector.connect(host="localhost", user="root", password="Asdf124#")
if (db):
    print("connected")
else:
    print("fail")
1
  • Is this Answers your question ? Commented Feb 8, 2020 at 10:32

1 Answer 1

0

As from examples in the docs:

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()

This is the proper way to handle exceptions when connecting to the database.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.