Python MySQL Create Database

📔 : Python 🔗

तो जैसा कि आपको पता है कि MySQL Structured Query Language है , मतलब database से interact करने और data fetch करने के लिए आपको diff - diff commands run करनी पड़ेगी।


Python में MySQL Database में database create करने के लिए नीचे दी गयी command को run करना होगा करनी होगी।

create database database_name

MySQL case insensitive language है मतलब same query को आप capital या small letters में भी लिख सकते हो।

Python MySQL Create Database Example

database create करने के लिए सबसे पहले तो आपको connection बनाना पड़ेगा।

Copy Fullscreen Close Fullscreen
import mysql.connector as myconn
mydb = myconn.connect(
  host = 'localhost',
  user = 'root',
  password = ''
)
db_cursor = mydb.cursor()
db_cursor.execute('create database python_db')

अगर ऊपर दिया गया code बिना किसी error के run हुआ तो database create हो चुका है।

Python List All Databases

सभी databases की listing करने के लिए आपको show databases command run करनी पड़ेगी। फिर for loop का use करके आप सभी database की listing कर सकते हैं।

Copy Fullscreen Close Fullscreen Run
import mysql.connector as myconn
mydb = myconn.connect(
  host = 'localhost',
  user = 'root',
  password = ''
)
db_cursor = mydb.cursor()
db_cursor.execute('show databases')
for db in db_cursor : print(db)
Output
C:\Users\Rahulkumar\Desktop\python>python list_dbs.py
('db_mylearningpoint',)
('db_test',)
('information_schema',)
('mysql',)
('performance_schema',)
('phpmyadmin',)
('procapitas',)
('python_db',)
('test',)

Python MySQL Delete Database

Table को rename करने के लिए भी बाकी procedure तो same रहता है बस execute method men Query change हो जायगी।

import mysql.connector as myconn
mydb = myconn.connect(
  host = 'localhost',
  user = 'root',
  password = '',
  database = 'python_db'
)
db_cursor = mydb.cursor()
query = "drop database database_name"
db_cursor.execute(query)

यहाँ database_name में उस database से replace कर दें जिसे आपको delete करना हो।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers