← Back to Python Course | Chapter 15: Python & MySQL | Lesson 2 of 12

Python MySQL Create Database

एक database खुद -- वह top-level container जो आपकी सभी tables रखता है -- सिर्फ किसी database admin tool से नहीं, बल्कि Python से भी programmatically बनाया जा सकता है। MySQL server से connection होने के बाद, cursor object के ज़रिए एक CREATE DATABASE SQL statement चलाना ठीक यही करता है।
Syntax
python
cursor = connection.cursor()
cursor.execute("CREATE DATABASE database_name")

एक Cursor बनाना

conn.cursor() से मिलने वाला cursor वह object है जो असल में SQL statements execute करने और results वापस लाने के लिए इस्तेमाल होता है -- connection object database server से link को represent करता है, जबकि cursor वह है जो उस link से commands भेजता है।

Note: सीधे-सादे scripts के लिए हर connection पर एक cursor बनाएँ -- जब तक आप genuinely concurrent operations न चला रहे हों, आमतौर पर एक से ज़्यादा की ज़रूरत नहीं होती।
Warning: सीधे connection object पर (उससे मिले cursor की बजाय) query execute करने की कोशिश करना एक आम beginner गलती है -- connection के पास खुद कोई .execute() method नहीं होता।

उदाहरण: Creating a Cursor

python
from unittest.mock import MagicMock

conn = MagicMock()
cursor = conn.cursor()  # cursor is what actually sends SQL commands
print(type(cursor))

एक Database बनाना

cursor.execute("CREATE DATABASE dbName") cursor के ज़रिए CREATE DATABASE SQL command भेजता है -- इसके लिए इस्तेमाल होने वाले connection को पहले से किसी particular database का नाम देने की ज़रूरत नहीं, क्योंकि CREATE DATABASE किसी existing database के अंदर नहीं, बल्कि server level पर काम करता है।

Note: जब आपकी script का काम specifically ऐसा database बनाना है जो अभी मौजूद नहीं है, तो बिना कोई specific database नाम दिए connect करें (database argument छोड़ दें)।
Warning: Database बनाने के लिए connect करने वाले MySQL user के पास CREATE privileges होने चाहिए -- एक restricted application-level user के पास अक्सर यह नहीं होता और setup scripts के लिए एक admin account चाहिए होता है।

उदाहरण: Creating a Database

python
from unittest.mock import MagicMock

conn = MagicMock()
cursor = conn.cursor()
cursor.execute("CREATE DATABASE mydatabase")  # runs at the server level, no database needs to be selected first
print("Database created")

IF NOT EXISTS से Errors से बचना

CREATE DATABASE IF NOT EXISTS dbName statement को एक से ज़्यादा बार चलाने के लिए सुरक्षित बनाता है -- database पहले से मौजूद होने पर error raise करने के बजाय, MySQL बस कुछ नहीं करता और script को चलने देता है, जो उन setup scripts के लिए उपयोगी है जो कई बार चल सकती हैं।

Note: किसी भी ऐसी setup script में IF NOT EXISTS इस्तेमाल करें जो सुरक्षित रूप से दोबारा चलाई जा सके, जैसे कोई installer या first-run initialization routine।
Warning: अगर database पहले से मौजूद है तो IF NOT EXISTS चुपचाप कुछ नहीं करता -- अगर किसी existing database की structure आपकी उम्मीद से अलग है तो यह आपको warn नहीं करेगा।

उदाहरण: Avoiding Errors with IF NOT EXISTS

python
from unittest.mock import MagicMock

conn = MagicMock()
cursor = conn.cursor()
cursor.execute("CREATE DATABASE IF NOT EXISTS mydatabase")  # does nothing instead of erroring if it already exists
print("Safe to run more than once")

सीधे एक Specific Database से Connect करना

एक बार database मौजूद हो जाए, तो बाद के connections mysql.connector.connect() में database argument के ज़रिए सीधे उसका नाम दे सकते हैं, जो पहले generically connect करके फिर USE statement से अलग database switch करने से ज़्यादा सरल है।

Note: ऐसी scripts के लिए जो हमेशा सिर्फ एक specific, पहले से बने database के साथ काम करती हैं, generically connect करके बाद में switch करने की बजाय शुरू से सीधे उससे connect करें।
Warning: ऐसे database argument से connect करना जो अभी मौजूद नहीं है तुरंत एक error raise करता है -- database पहले से बना होना चाहिए।

उदाहरण: Connecting Directly to a Specific Database

python
from unittest.mock import MagicMock

connect = MagicMock(return_value=MagicMock())
conn = connect(host="localhost", user="root", password="", database="mydatabase")  # database named directly
print("Connected directly to mydatabase")

Character Set और Collation चुनना

CREATE DATABASE dbName CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci database creation के समय default character encoding और sorting rules सेट करता है -- utf8mb4 modern recommended choice है क्योंकि यह emoji सहित पूरे Unicode range को support करता है।

Note: Database creation के समय हमेशा explicitly utf8mb4 सेट करें; MySQL में पुराना utf8 charset असल में एक restricted subset है जो हर Unicode character store नहीं कर सकता।
Warning: बाद में बनाई गई tables individually database के default character set को override कर सकती हैं, इसलिए database-level setting एक sensible default है, हर table के लिए absolute guarantee नहीं।

उदाहरण: Choosing Character Set and Collation

python
from unittest.mock import MagicMock

conn = MagicMock()
cursor = conn.cursor()
cursor.execute("CREATE DATABASE mydatabase CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci")  # utf8mb4 supports full Unicode, including emoji
print("Database created with full Unicode support")
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.