Python Database API ( Application Program Interface ) is the Database interface for the standard Python. This standard is adhered to by most Python Database interfaces. There are various Database servers supported by Python Database such as MySQL, GadFly, mSQL, PostgreSQL, Microsoft SQL Server 2000, Informix, Interbase, Oracle, Sybase etc. To connect with MySQL database server from Python, we need to import the mysql.connector interface.
Syntax:
CREATE DATABASE DATABASE_NAME
Example:
Python
import mysql.connector
dataBase = mysql.connector.connect(
host = "localhost" ,
user = "user" ,
passwd = "gfg"
)
cursorObject = dataBase.cursor()
cursorObject.execute( "CREATE DATABASE geeks4geeks" )
|
Output:

The above program illustrates the creation of MySQL database geeks4geeks in which host-name is localhost, the username is user and password is gfg.
Let’s suppose we want to create a table in the database, then we need to connect to a database. Below is a program to create a table in the geeks4geeks database which was created in the above program.
Python
import mysql.connector
dataBase = mysql.connector.connect(
host = "localhost" ,
user = "user" ,
passwd = "gfg" ,
database = "geeks4geeks" )
cursorObject = dataBase.cursor()
studentRecord =
cursorObject.execute(studentRecord)
dataBase.close()
|
Output:

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
04 Jul, 2021
Like Article
Save Article