Jon4thanPP
Jon4thanPP

Reputation: 35

select_db in mysql.connector?

I created a database

import mysql.connector as mysql
create_database = "CREATE DATABASE IF NOT EXISTS " + self.database

and I want to select the database after creating it

 self.db = mysql.connect(
        host=self.host,
        port=self.port,
        user=self.user,
        passwd=self.passwd,

    )

    cursor = self.db.cursor()
    cursor.execute(create_database)
    self.db.select_db(self.database)

And comes the error :AttributeError: 'CMySQLConnection' object has no attribute 'select_db'

Can't find any answer about this using mysql.connector.

What is the exact way to select a database and make it in session? thanks.

Upvotes: 1

Views: 516

Answers (1)

Nuno Mariz
Nuno Mariz

Reputation: 591

You can use database (or the alias db) option in connect() if you already have a database.

In your case that you want to create and use it afterwards, you can use self.db.cmd_init_db(self.database).

Just keep in mind that you need to get another cursor, the one used to create the database doesn't have a default database.

Upvotes: 1

Related Questions