Mysql क्या है ?
Mysql एक Open-Source Relational Database Management System (RDBMS) है | जिसका उपयोग Data को perfectly store manage करने के लिए किया जाता है | Mysql मे data को Table की form मे store किया जाता है | जैसे
| roll no | st_name | st_fname |
|---|---|---|
| 1 | vinay | Raghu |
| 2 | Pramod | Ganesh |
| 3 | vikesh | Naresh |
जैसा की आप को पता ही होगा की web-development , app-development etc. के साथ Python का उपयोग किया जाता है , इसलिए Login system , customer orders आदि का data store करने के लिए Python के साथ Mysql का उपयोग किया जाता है | वैसे तो Mysql का उपयोग companies , schools , banking etc. मे किया जाता है | इसलिए Mysql को भी Python के जैसे अलग से सिखना पडता है | इसलिए इस Python के course मे बस basic Mysql को explain किया गया है | जैसे Mysql को Python से connect करना , Python के साथ backend मे data को store करना etc. Python के साथ Mysql का उपयोग करने के लिए नीचे दिए गए steps को follow किया जाता है |
1. Install Library
Install Library python के साथ Mysql का उपयोग करने का पहला और basic step है | इस step मे Mysql की python मे library install की जाती है | mysql-connector-python Library Python language के लिए बनाई गई है | इस library के उपयोग से mysql को Python के अलावा किसी दूसरी languages के साथ connect नही किया जा सकता है | ये library open source है | mysql-connector library का उपयोग करने के लिए इस library को अलग से pip की सहायता से install किया जाता है मतलब ये library python के साथ नही आती है |
How To Install mysql-connector-python Library
1. सबसे पहले cmd open करे | फिर
2. pip install mysql-connector-python command को type करे | फिर
3. अब enter करते ही library install होना शुरू हो जाएगी |
library के install होने के बाद cmd को close कर दे | आप इस library को cmd के अलावा Python के terminal से भी install कर
सकते है | उसके लिए भी same steps है | बस cmd की जगह python terminal open करे |
1. mysql-connector-python library Mysql को Python के साथ connect करती है |
2. Python को Mysql queries/code run करने देती है |
3. app , web etc. के data को Mysql मे store करने देती है |
4. Mysql data को Python programs मे लाने देती है |
Mysql workbench
mysql workbench MySQL की official GUI tool है | जिसका उपयोग database create करने , update करने etc. के लिए किया जाता है | Python से table की form मे data को store करने के बाद data को table की form मे देखने के लिए mysql workbench का उपयोग किया जाएगा | mysql workbench को install करने लिए नीचे दिए गए steps को follow करे |
How To Install Mysql workbench
1. mysql की official websites https://dev.mysql.com/downloads/workbench/ से mysql workbench को install करे |
2. download होने के बाद .msi या .exe file खोलें |Installation wizard open होगा
Next → Typical → Next → Install
3. launch करे |
अब mysql workbench को mysql से connect करे |
Open Workbench “+” symbol पर क्लिक करें
Connection का नाम डालें (जैसे Localhost)
Hostname: localhost करे/fill करे |
Username: root रहने दे |
Password अपना एक strong डालें |
2. connection
Mysql library को install कर लेने के बाद python को Mysql से connect किया जाता है | mysql का use क र ने के लि ए ये step बहुत महात्वपूर्ण भूमिका निभाता है | Because इस step मे mistake करने पर mysql python से connect नही हो पाएगा | mysql को python से connect करने के लिए नीचे दिए गए code का उपयोग किया जाता है |
import mysql.connector
con = mysql.connector.connect(
host="localhost",
user="root",
password="1234",
database="student"
)
Syntax Explanation
import mysql.connector उपर जो mysql library install की गई है | उस library को
को python file मे import करने के लिए import mysql.connector लिखा गया है |
con = mysql.connector.connect( con से connection बनाया जा रहा है | जो details
लिखी गई है | वो बता रही है कि किस तरह से connect करना होगा |
host="localhost", मतलब mysql कहा पर install है |
user="root" mysql का username
password="1234" ये mysql का password है |
database="student" mysql मे student database पर काम किया जा रहा है |
NOTE : इस connection मे बस अपने password को ही change करे | बाकी code मे change न करे | कैसा database create करना
उसके according database choose करे | जैसे hospital
3. Cursor
Cursor एक pointer होता है | जिसका उपयोग mysql की queries को execute करने के लिए किया
जाता है , यानि python mysql से directly बात नही कर सकता है | इसलिए cursor mysql और
python को आपस मे जोडता है |
Python से mysql की insert , update , delete etc. commands को execute करने के लिए cursor का उपयोग किया
जाता है | Python मे cursor एक object का काम करता है , यानि cursor के बीना लिखे गए code का कोई महत्व
नही होता | cursor को बनाने का code नीचे दिया गया है : -
con = mysql.connector.connect(
host="localhost",
user="root",
password="1234",
database="school"
)
cur = con.cursor()
Syntax Explanation
cursor को बनाने से पहले Python और mysql के बीच connection बनाया जाता है |
cur = con.cursor() con.cursor() से एक cur नाम का cursor बनाया गया है | अगर
अब देखा जाए तो cursor के उपर लिखा गया code किसी काम का नही है | जब तक cursor न बनाया जाए ,
Because cursor से ही mysql queries को run किया जाता है |
4. Table Create
mysql मे data table की form मे store किया जाता है | लेकिन table की form मे data को store करने से पहले mysql मे table के Structure को समझना जरूरी है | नीचे एक imgae की सहायता से table के part को समझया गया है |
Attributes mysql मे table के columns को attribute कहा जाता है |
attributes ये बताते है कि table मे किस तरह का data store किया जाएगा | ऊपर दी गई table मे Roll no. , student_name
और student_class etc. table के attributes है | किसी table मे data उसके attributes के according
ही store किया जाता है | यदि किसी table मे stored data table के attributes के according नही है | तो
उस table मे stored data का कोई meaning नही होता है |
Tupple table की एक पूरी row को Tupple कहा जाता है | ऊपर दी गई table मे
1 , Rahul , 12th table की Tupple है | Tupple मे attributes के according values store की जाती है |
Field = Row + Column का intersection होती है | ऊपर दी गई table मे 1 (id का field) ,
Ram (name का field) ,
20 (age का field) है |

1. CREATE Command
आप को पता ही है कि mysql मे data को table की form मे store किया जाता है | create command mysql की सबसे पहली , basic और Important command है | Because Create command के उपयोग से table को create किया जाता है | जिस तरह से create command लिखी जाएगी उसी तरह से table का Structure create होगा | एक table create करने के लिए create command नीचे लिखी गई है : -
1import mysql.connector 2con = mysql.connector.connect( 3 host="localhost", 4 user="root", 5 password="__", 6 database="school" 7) 8cur = con.cursor() 9 10cur.execute(""" 11CREATE TABLE IF NOT EXISTS students ( 12 id INT PRIMARY KEY AUTO_INCREMENT, 13 name VARCHAR(50), 14 age INT, 15 class VARCHAR(20) 16) 17""") 18print("Table Created Successfully") 19con.commit()
Syntax Explanation
import mysql.connector python file मे mysql की library को import किया गया है |
con = mysql.connector.connect( mysql और Python को connect किया गया है |
database="school mysql मे school नाम का database Create जाएगा |
password="__" यहा अपने mysql workbench का password दे |
cur = con.cursor() insert , Create , update etc. commands को run करने के लिए cur नाम
का cursor Create किया गया है |
cur.execute(""" लिखा गया है कि school database को execute/run करो |
CREATE TABLE IF NOT EXISTS students ( Create command का उपयोग किया गया है | student नाम table Create करने
के लिए फिर table के attributes defin किए गए है |
id INT PRIMARY KEY AUTO_INCREMENT id नाम का एक attribute Create किया गया | जिसकी value
अपने आप increase होती जाएगी |
name VARCHAR(50) name नाम का attribute Create किया गया है जिसमे 50 character तक का
ही name store हो सकता है |
class VARCHAR(20) class नाम का attribute Create किया गया है जिसमे 20 character तक का
ही class name store हो सकता है |
print("Table Created Successfully") जब आप Python file मे ये code डालकर code को run
करोगे तो print Functions मे लिखा गया message output मे display होगा |
अब mysql workbench मे school नाम के database मे students नाम की table को search करे जैसे ही students नाम की table को
open करोगे तो एक table Create हुई मिलेगी जैसे :-
2. commit Command
commit command का use ये बताने के लिए किया जाता है कि बाद मे table के साथ और काम किया जायेगा | python के साथ commit
command का use करने के लिए commit command को कुछ ऐसे लिखा जाता है |
con.commit()
मतलब mysql और python के connection को commit करो |
commit command का उपयोग python code के last मे किया जाता है |
3. insert command
आप देख पा रहे होगे की students नाम की table Create हो गयी है , लेकिन table के attributes मे value null है | इसलिए table के attributes मे value store करने के लिए insert command का उपयोग किया जाता है | insert command से table के attribute मे value add करने के लिए insert command का code नीचे दिया गया है |
1import mysql.connector 2con = mysql.connector.connect( 3 host="localhost", 4 user="root", 5 password="__", 6 database="school" 7) 8cur = con.cursor() 9 10cur.execute(""" 11INSERT INTO students (name, age, class) 12VALUES (%s, %s, %s) 13""", ("Ram", 20, "12th")) 14print("Data Inserted Successfully")
cur.execute(""" cur को table को execute करने के लिए कहा गया है |
INSERT INTO students (name, age, class) students table के (name, age, class) attributes
मे insert करने को कहा गया है |
VALUES (%s, %s, %s) attributes मे values assign करने के %s का उपयोग किया जाता है |
("Ram", 20, "12th") attributes मे values insert की गई है |
code को run करने पर Data Inserted Successfully लिखा आएगा पर workbench table मे insert हुआ नही मिलेगा |
4. select command
select command table मे data को update करने के बाद select command table मे updated data को show करवाती है | यानि table को execute करने के लिए select command का उपयोग किया जाता है | इसलिए insert किए हुए data को students table मे लाने के लिए select command का उपयोग किया जाएगा | select command का code नीचे दिया गया है |
1import mysql.connector 2con = mysql.connector.connect( 3 host="localhost", 4 user="root", 5 password="__", 6 database="school" 7) 8cur = con.cursor() 9cur.execute("SELECT * FROM students") 10rows = cur.fetchall()
Syntax Explanation
cur.execute("SELECT * FROM students") cursor students table से execute करो |
rows = cur.fetchall() fetchall Functions की help से सभी rows को |
अब mysql workbench मे table को execute करने पर table मे data insert हुआ मिलेगा | execute button का photo कुछ (⚡) ऐसा होता है |
create की गई table मे कुछ और values add कर ले ताकि आगे की commands को समझने मे आसानी हो |
5. update command
table मे stored data मे कुछ update करने के लिए update command का उपयोग किया जाता है | supose करे की आपने name गलत डाल दिया | तो name को update करने के लिए update command का उपयोग करे | update command का code नीचे दिया गया है |
1import mysql.connector 2con = mysql.connector.connect( 3 host="localhost", 4 user="root", 5 password="__", 6 database="school" 7) 8cur = con.cursor() 9cur.execute(""" 10UPDATE students 10SET name = 'Shyam' 10WHERE id = 1 10""") 10con.commit() 10print("Name Updated Successfully!")
Syntax Explanation
cur.execute(""" cursor execute करो |
UPDATE students students table को update मे update करो |
SET name = 'Shyam' name attribute मे stored value को Shyam set करो |
WHERE id = 1 where name की id 1 है |
6. Delete Command
किसी row को , value को , attribute etc. को delete करने के लिए Delete command का use किया जाता है | जैसे : -
1import mysql.connector 2con = mysql.connector.connect( 3 host="localhost", 4 user="root", 5 password="__", 6 database="school" 7) 8cur = con.cursor() 9 10cur.execute("DELETE FROM students WHERE id = 2") 11con.commit() 12print("Row Deleted Successfully!") 13cur.execute("SELECT * FROM students") 14print(cur.fetchall())
Syntax Explanation
cur.execute("DELETE FROM students WHERE id = 2") cursor execute करो students table मे where id 2 है delete करो |
7. Alter command
alter command के use कोई column row add किए जा सकते है | जै से :-
1import mysql.connector 2con = mysql.connector.connect( 3 host="localhost", 4 user="root", 5 password="__", 6 database="school" 7) 8cur = con.cursor() 9 10dis = "ALTER TABLE studentsss ADD COLUMN district VARCHAR(50) AFTER class" 11cur.execute(dis) 12con.commit() 13print("Column 'district' added ")
8. Truncate Command
Truncate command के use से table और columns delete नही होते है लेकिन Truncate command table के सभी rows को delete कर देती है जैसे :-
1import mysql.connector 2con = mysql.connector.connect( 3 host="localhost", 4 user="root", 5 password="__", 6 database="school" 7) 8cur = con.cursor() 9 10cur.execute("TRUNCATE TABLE studentsss") 11con.commit() 12print("code Successfully run")
9. Drop Command
Drop command के use पूरी table drop हो जाती है , यानि delete हो जाती है |
1import mysql.connector 2con = mysql.connector.connect( 3 host="localhost", 4 user="root", 5 password="__", 6 database="school" 7) 8cur = con.cursor() 9 10cur.execute("TRUNCATE TABLE studentsss") 11con.commit() 12print("code Successfully run")
Python के साथ MySQL का Purpose
हम python के साथ MySQL का use क्यों करे हमारा Python के साथ mysql को use करने का Purpose क्या है | इस को समझ लेना बहुत जरूरी है | ताकि आप अपने Purpose के according mysql skills को और improve कर सके | Python के साथ mysql का use करने के छ Purpose को नीचे दिए गए है |
1. Python की मदद से कोई भी real-world data MySQL database में save कर सकते हैं।
2. mysql की delete , update , insert जैसी commands के use से data को perfectly store manage किया जा सकता है |
3. backend मे Pyton के साथ websites , apps etc. के data को store कर सकते है |
4. डेटा को database में store करके Python ML model में use करना आसान होता है।
5. banking , gaming companies , hospitals etc. मे mysql का बहुत अधिक use किया जा रहा है |
Advantages Of Mysql With Python
1. बहुत ही आसानी से mysql को Python से connect किया सकता है |
2. Python के साथ mysql का उपयोग कर पाना सरल है |
3. Python और mysql से किसी website app etc. का backend मे data store किया जा सकता है |
4. Python और mysql को आपस connect करके mac , window , linux सब पर काम किया जा सकता है |
5. mysql मे लाखों rows मे data को store किया जा सकता है |
अध्याय समाप्त — आगे बढ़ें
आपने इस chapter मे python के बहुत ही महत्वपूर्ण concepts समझे है जो आगे की प्रोग्रामिंग सीखने मे बहुत Helpful हैं। आगे के chapter मे आप Python Libraries के बारे मे सिखेगे आगे के chapter की और बढ़ने के लिए नीचे दिए गए बटन पर क्लिक करे |
Next Chapter
Seekho Coding