I am trying to connect to a MYSQL instance using mysql.connector
for python. Once the initial connection is established, I want to connect to the database as input by the user.
Here is my implementation:
...
import mysql.connector as sqlcon
connection = sqlcon.connect(
user=conn_info['user'], password=conn_info['password'], host=conn_info['host'], auth_plugin='mysql_native_password')
#conn_info is a dictionary holding the values
cursor = connection.cursor()
db_name = input("Enter db name")
...
Is there a way to connect to db_name
after this step?
CodePudding user response:
From the documentation:
To change the current database later, execute a
USE
SQL statement or set thedatabase
property of theMySQLConnection
instance.
So you can use:
connection.database = db_name
to connect to the user-selected database.