Python MySQL Get Started
In this page:
import mysql.connector
connection = mysql.connector.connect(
host="host", user="user", password="password"
)
MySQL Connector Install करना
pip install mysql-connector-python official MySQL driver package download और install करता है, जिससे आपकी scripts में import करने के लिए mysql.connector module उपलब्ध हो जाता है -- यह आप जिस भी Python environment (या virtual environment) में काम कर रहे हैं, उसमें एक बार का setup step है।
उदाहरण: Installing the MySQL Connector
# pip install mysql-connector-python
print("Installs the mysql.connector module for your Python environment")
एक Basic Connection बनाना
mysql.connector.connect(host="localhost", user="root", password="", database="mydatabase") दी गई credentials से MySQL server से एक connection खोलता है, और हर आगे की query के लिए इस्तेमाल होने वाला एक connection object return करता है।
उदाहरण: Establishing a Basic Connection
from unittest.mock import MagicMock
# Stands in for mysql.connector.connect(), which needs a real MySQL server
connect = MagicMock(return_value=MagicMock())
conn = connect(host="localhost", user="root", password="", database="mydatabase")
print(type(conn))
Connection Errors को Handle करना
mysql.connector.Error वह base exception class है जो connection attempt fail होने पर raise होती है -- इसे try-except block में catch करने से आप एक साफ error message दिखा सकते हैं और gracefully fail हो सकते हैं, बजाय इसके कि किसी unhandled exception से script crash हो जाए।
उदाहरण: Handling Connection Errors
from unittest.mock import MagicMock
class Error(Exception):
pass
# Stands in for mysql.connector.connect() failing with mysql.connector.Error
connect = MagicMock(side_effect=Error("Access denied"))
try:
connect(host="localhost", user="root", password="wrong")
except Error as e:
print("Connection failed:", e)
Credentials को Securely Store करना
Database credentials को कभी भी सीधे किसी ऐसी script में hardcode नहीं करना चाहिए जो version control में commit होती है -- उन्हें environment variables से (os.environ या python-dotenv package इस्तेमाल करके) load करना sensitive values को आपके codebase की history से पूरी तरह बाहर रखता है।
उदाहरण: Storing Credentials Securely
import os
db_user = os.environ.get("DB_USER", "root") # read from the environment, not hardcoded
db_password = os.environ.get("DB_PASSWORD", "")
print("Loaded credentials from environment, not hardcoded")
एक Connection को Close करना
conn.close() किसी database connection की ज़रूरत खत्म होने पर उसे explicitly close कर देता है, जिससे Python side और MySQL server दोनों पर resource free हो जाता है -- यह लंबे समय तक चलने वाली scripts या कई short-lived connections खोलने वाली scripts में खासकर महत्वपूर्ण है।
उदाहरण: Closing a Connection
from unittest.mock import MagicMock
connect = MagicMock(return_value=MagicMock())
conn = connect(host="localhost")
conn.close() # releases the connection when it's no longer needed
print("Connection closed")
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