Python MySQL Create Database
In this page:
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 भेजता है।
उदाहरण: Creating a Cursor
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 पर काम करता है।
उदाहरण: Creating a Database
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 के लिए उपयोगी है जो कई बार चल सकती हैं।
उदाहरण: Avoiding Errors with IF NOT EXISTS
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 करने से ज़्यादा सरल है।
उदाहरण: Connecting Directly to a Specific Database
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 करता है।
उदाहरण: Choosing Character Set and Collation
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")
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first:
- Python MySQL Get Started
- Python MySQL Create Database
- Python MySQL Create Table
- Python MySQL Insert Into Table
- Python MySQL Select From Table
- Python MySQL Select With a Filter
- Python MySQL Order By
- Python MySQL Delete Record
- Python MySQL Drop Table
- Python MySQL Update Table
- Python MySQL Limit
- Python MySQL Join