Skip to content
  • SiteMap
  • Our Services
  • Frequently Asked Questions (FAQ)
  • Support
  • About Us

UpdateGadh

Update Your Skills.

  • Home
  • Projects
    •  Blockchain projects
    • Python Project
    • Data Science
    •  Ai projects
    • Machine Learning
    • PHP Project
    • React Projects
    • Java Project
    • SpringBoot
    • JSP Projects
    • Java Script Projects
    • Code Snippet
    • Free Projects
  • Tutorials
    • Ai
    • Machine Learning
    • Advance Python
    • Advance SQL
    • DBMS Tutorial
    • Data Analyst
    • Deep Learning Tutorial
    • Data Science
    • Nodejs Tutorial
  • Blog
  • Contact us
  • Toggle search form
Creating New Databases in MySQL Using Python - Creating New Database 

Creating New Databases in MySQL Using Python

Posted on December 20, 2024December 20, 2024 By Rishabh saini No Comments on Creating New Databases in MySQL Using Python

Creating New Database 

Databases form the backbone of any application by securely storing and managing data. In this guide, we’ll explore how to create a new database in MySQL using Python. Along the way, we’ll look at listing existing databases and adding a new one to your database server.

 

Download New Real Time Projects :-Click here

Getting the List of Existing Databases

Before creating a new database, it’s helpful to view the databases already present on the MySQL server. This can be achieved using the SHOW DATABASES; SQL query. Below is the Python implementation for retrieving the list of existing databases:

Example: Viewing Existing Databases

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:  
    # Execute the query to fetch database names  
    cur.execute("SHOW DATABASES")  
except:  
    myconn.rollback()  
  
# Displaying the databases  
for x in cur:  
    print(x)  
  
# Closing the connection  
myconn.close()  

Output

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

The SHOW DATABASES command lists all the databases currently present in the MySQL server.


Creating a New Database

To create a new database, you can use the CREATE DATABASE <database-name> SQL command. The process is straightforward, and the new database will appear in the list of databases once it’s created successfully.

Example: Creating a New Database

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")  
  
    # Fetching the updated list of databases  
    cur.execute("SHOW DATABASES")  
except:  
    myconn.rollback()  
  
# Displaying the updated list of databases  
for x in cur:  
    print(x)  
  
# Closing the connection  
myconn.close()  

Output

('EmployeeDB',)  
('PythonDB2',)  
('Test',)  
('TestDB',)  
('information_schema',)  
('javatpoint',)  
('mydb',)  
('mysql',)  
('performance_schema',)  
('testDB',)  

Explanation

  1. Connecting to MySQL Server:
    The mysql.connector.connect() function establishes a connection to the MySQL server. Ensure that the host, user, and password parameters match your MySQL configuration.

  2. Creating a Cursor Object:
    A cursor object is created using the myconn.cursor() method. It allows you to execute SQL queries.

  3. Executing SQL Queries:

    • The SHOW DATABASES query fetches the list of all databases.
    • The CREATE DATABASE PythonDB2 query creates a new database named PythonDB2.
  4. Exception Handling:
    The try-except block ensures that the program gracefully handles errors, such as database creation failure.

  5. Closing the Connection:
    Closing the connection with myconn.close() is a best practice to free resources.


Updated Notes

  • Backward Compatibility: MySQL and Python are widely used together, and the provided examples are compatible with modern MySQL server versions and the mysql-connector-python library.
  • Future-Proof Your Code: Regularly update your Python libraries to ensure compatibility with the latest features and security patches.
PHP PROJECT:- CLICK HERE
 


how to create a database mysql
how to create a database in sql
how to create database in sql server
how to create a database in excel
create database mysql command
how to create a database for beginners
how to create a database in access
create table in sql
creating new database in sql server
creating new database w3schools
creating new database oracle
creating new database example
creating new database
Post Views: 812
Python Tags:create database, create new database in mysql, creating a database, creating a database from scratch, creating a database in excel, database, database design, databases, excel database, how to create a database, introduction to databases, mysql create database, MySQL Database, reading from a database, relational database, relational database design, relational database design tutorial, relational databases for beginners, what is database

Post navigation

Previous Post: Weather Information App
Next Post: PHP Contact Form with PHPMailer

More Related Articles

Fetcher App Fetcher App Using Python and Tkinter Python
Python Classes and Objects: A Guide to Mastering Object-Oriented Programming - Python Classes and Objects Python Classes and Objects: A Guide to Mastering Object-Oriented Programming Python
Python OpenCV Object Detection: A Step-by-Step Guide - Python OpenCV Object Detection Python OpenCV Object Detection: A Step-by-Step Guide Python

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may also like

  1. Python Decorators: A Comprehensive Guide
  2. How to Calculate Distance between Two Points using GEOPY
  3. Rock, Paper, Scissors Game
  4. Database Operations: UPDATE and DELETE in MySQL Using Python
  5. How to Install Django: Step-by-Step Guide
  6. Python Tkinter Canvas: A Guide to Structured Graphics in Python

Most Viewed Posts

  1. Top Large Language Models in 2025
  2. Online Shopping System using PHP, MySQL with Free Source Code
  3. login form in php and mysql , Step-by-Step with Free Source Code
  4. Flipkart Clone using PHP And MYSQL Free Source Code
  5. News Portal Project in PHP and MySql Free Source Code
  6. User Login & Registration System Using PHP and MySQL Free Code
  7. Top 10 Final Year Project Ideas in Python
  8. Online Bike Rental Management System Using PHP and MySQL
  9. E learning Website in php with Free source code
  10. E-Commerce Website Project in Java Servlets (JSP)
  • AI
  • ASP.NET
  • Blockchain
  • ChatCPT
  • code Snippets
  • Collage Projects
  • Data Science Project
  • Data Science Tutorial
  • DBMS Tutorial
  • Deep Learning Tutorial
  • Final Year Projects
  • Free Projects
  • How to
  • html
  • Interview Question
  • Java Notes
  • Java Project
  • Java Script Notes
  • JAVASCRIPT
  • Javascript Project
  • JSP JAVA(J2EE)
  • Machine Learning Project
  • Machine Learning Tutorial
  • MySQL Tutorial
  • Node.js Tutorial
  • PHP Project
  • Portfolio
  • Python
  • Python Interview Question
  • Python Projects
  • PythonFreeProject
  • React Free Project
  • React Projects
  • Spring boot
  • SQL Tutorial
  • TOP 10
  • Uncategorized
  • Online Examination System in PHP with Source Code
  • AI Chatbot for College and Hospital
  • Job Portal Web Application in PHP MySQL
  • Online Tutorial Portal Site in PHP MySQL — Full Project with Source Code
  • Online Job Portal System in JSP Servlet MySQL

Most Viewed Posts

  • Top Large Language Models in 2025 (8,612)
  • Online Shopping System using PHP, MySQL with Free Source Code (5,211)
  • login form in php and mysql , Step-by-Step with Free Source Code (4,866)

Copyright © 2026 UpdateGadh.

Powered by PressBook Green WordPress theme