TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Python MySQL Creating New Database

Python MySQL Creating New Database with python tutorial, overview, environment set-up, first python program, basics, data types, operators, if-else statements, loops, history, features, history, versions, example, operators, variables etc.

<< Back to PYTHON

Creating new databases

In this section of the tutorial, we will create the new database PythonDB.

Getting the list of existing databases

We can get the list of all the databases by using the following MySQL query.

>  show databases;

Example

import mysql.connector

#Create the connection object 
myconn = mysql.connector.connect(host = "localhost", user = "root",passwd = "google")

#creating the cursor object
cur = myconn.cursor()

try:
    dbs = cur.execute("show databases")
except:
    myconn.rollback()
for x in cur:
    print(x)
myconn.close()

Output:

('EmployeeDB',)
('Test',)
('TestDB',)
('information_schema',)
('TheDeveloperBlog',)
('TheDeveloperBlog1',)
('mydb',)
('mysql',)
('performance_schema',)
('testDB',)

Creating the new database

The new database can be created by using the following SQL query.

>  create database <database-name>  

Example

import mysql.connector

#Create the connection object 
myconn = mysql.connector.connect(host = "localhost", user = "root",passwd = "google")

#creating the cursor object
cur = myconn.cursor()

try:
    #creating a new database
    cur.execute("create database PythonDB2")

    #getting the list of all the databases which will now include the new database PythonDB
    dbs = cur.execute("show databases")
    
except:
    myconn.rollback()

for x in cur:
        print(x)
        
myconn.close()

Output:

('EmployeeDB',)
('PythonDB',)
('Test',)
('TestDB',)
('anshika',)
('information_schema',)
('TheDeveloperBlog',)
('TheDeveloperBlog1',)
('mydb',)
('mydb1',)
('mysql',)
('performance_schema',)
('testDB',)
Next TopicCreating Tables




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf