Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Python MySQL - Update Query
Next article icon

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
# importing required librariesimportmysql.connectordataBase=mysql.connector.connect(host="localhost",user="user",passwd="gfg")# preparing a cursor objectcursorObject=dataBase.cursor()# creating databasecursorObject.execute("CREATE DATABASE geeks4geeks")

Output:
 

python-mysql-create-db


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
# importing required libraryimportmysql.connector# connecting to the databasedataBase=mysql.connector.connect(host="localhost",user="user",passwd="gfg",database="geeks4geeks")# preparing a cursor objectcursorObject=dataBase.cursor()# creating tablestudentRecord="""CREATE TABLE STUDENT (                   NAME  VARCHAR(20) NOT NULL,                   BRANCH VARCHAR(50),                   ROLL INT NOT NULL,                   SECTION VARCHAR(5),                   AGE INT                   )"""# table createdcursorObject.execute(studentRecord)# disconnecting from serverdataBase.close()

Output:
 

PYTHON-MYSQL-CREATE-DB1


 


Improve
Article Tags :
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp