How to Create Database in Python
This article deals with the creation of MySQL database in Python.
To create a database in MySQL, we can use the “CREATE DATABASE” statement. Before that, we need to import mysql.connector
and provide the required connection.
import mysql.connector my_db = mysql.connector.connect( host = "localhost", user = "yourusername", password = "yourpassword" ) my_cursor = my_db.cursor() my_cursor.execute("CREATE DATABASE mydatabase")
If the above code was executed with no errors, then you have successfully created a database.
You can check if a database exist by listing all databases in your system by using the “SHOW DATABASES” statement:
import mysql.connector my_db = mysql.connector.connect( host = "localhost", user = "yourusername", password = "yourpassword" ) my_cursor = my_db.cursor() my_cursor.execute("SHOW DATABASES") for x in my_cursor: print(x)
Subscribe
Login
Please login to comment
0 Discussion