Loading ...
SeekhoCoding
Python Syllabus Game Python Projects

Chapter 15 : Core Concepts Of Mysql With Python (Practice Sets)

Backend Create करने के लिए Python Language के साथ DataBase का उपयोग का उपयोग किया जाता है | Mysql का उपयोग Python Language के साथ Database Create करने के लिए किया जाता है | Python Language के साथ Mysql का उपयोग करना एक महत्वपूर्ण Topic है | हम ने हमारी Site के इस Web Page पर Mysql के Core Concepts से संबंधित Problems Add की है | इन Problems को Solve करने से Mysql के हर छोटे - से - छोटे Concept को समझ पाएगे | इस Web Page पर दि गई Problems को Solve करने से पहले Mysql के बारे मे जरूर पडे |

Problem 1 :

जब कोई Business start किया जाता है तब Manager Employee को jobs देने से पहले Employees का accounts बनाते है जिसमे Employee id , name , salary etc. information होती है |
एक Pyhton Program लिखे जो ,

  1. Database मे Employees Name की Table create करे |
  2. Employees Table मे id , E_Name , E_Salary columns हो |

Code Explanation

  1. import mysql.connector = > MySql Library को Import किया गया है |
  2. conn = mysql.connector.connect( = > conn एक Variable है जिसमे mysql और Python के connection की details stored है |
  3. host="localhost" = > का मतलब है कि Database आपके अपने Computer पर चल रहा है
  4. user="root" = > ये Database का 'Username' है | MySQL में 'root' default admin user होता है।
  5. password="" = > Password खाली रखा गया है अगर आपके पास Password है तो enter करे |
  6. database="company" = > ये उस Database का name है जिस पर काम किया जाएगा |
  7. cur = con.cursor() = > cur एक Variable है | Python से Database queries को execute करने के लिए cursor बनाया गया है |
  8. CREATE TABLE employee = > company Database मे employee नाम की Table create की गई है |
  9. id , E_name ,E_salary = > आदि Columns employee Table मे Create किए गए है |
  10. print("Table Created Successfully") = > Program बीना error आए run हो जाने पर ये Message Show करता है "Table Created Successfully"

Problem 2 :

Python से DataBase के साथ काम करने के लिए सबसे पहले Python और Database को आपस मे Connect किया जाता है और फिर Python मे DataBase queries लिखी जाती है |
नीचे दिए गए Code Editor मे Python और Database के बीच connection बनाने का Program लिखे |

Code Explanation

  1. import mysql.connector = > MySql Library को Import किया गया है |
  2. conn = mysql.connector.connect( = > conn एक Variable है जिसमे mysql और Python के connection की details stored है |
  3. host="localhost" = > का मतलब है कि Database आपके अपने Computer पर चल रहा है
  4. user="root" = > ये Database का 'Username' है | MySQL में 'root' default admin user होता है।
  5. password="" = > Password खाली रखा गया है अगर आपके पास Password है तो enter करे |
  6. database="company" = > ये उस Database का name है जिस पर काम किया जाएगा |

Problem 3 :

company Manager company मे किसी नए Employee के add होते ही employees के accounts मे नए Employee की entery add कर देता है ताकि नए Employees को भी old employees जैसा ही experience मिले |
एक Python Program लिखे जो ,

  1. DataBase की "employee" Table को Target करे |
  2. employee Table मे Any 4 new employees का data insert/add करे |

Code Explanation

  1. cur.execute(""" = > मतलब cursor execute करो |
  2. INSERT INTO employee (id, E_name, E_salary) = > employee Table के इन id, E_name, E_salary Columns मे data insert करो |
  3. VALUES (%s, %s, %s) = > Table के Columns मे VALUES %s के according add होगी |
  4. ("1", "Ram", 2000)) = > Table के Columns wise ये values add करो |
  5. con.commit() = > किए गए बदलावो को save करो |
  6. print("Data Inserted Successfully") = > Program के error free run हो जाने पर ये Data Inserted Successfully Show होगा |

Problem 4 :

company Manager किसी भी Employee की salary से संबंधित information को पता करने के लिए Employee के account से जानकारी प्राप्त करता है |
नीचे एक Program दिया गया है जो "employee" Table मे stored सारे Data को show करता है | लेकिन दिए गए Program को run करने पर error आ रहा है आपको नीचे दिए गए Program से error को remove करके "employee" Table के सारे Data को show करना है |

Code Explanation

  1. cur.execute("SELECT * FROM employee") = > cur employee Table को execute करो |
  2. con.commit() = > किए गए बदलावो को save करो |
  3. print(cur.fetchall()) = > employee Table मे stored सारा data print करो |

Problem 5 :

अगर गलती से किसी Employee के data मे mistake हो जाती है तो Manager बडी आसानी से उस mistake को ठीक कर देता है |
नीचे दिए गए Code Editor मे एक Program लिखे जो ,

  1. Database "employee" Table को Target करे |
  2. Employee Table के Id 1 के E_name को बदले |

Code Explanation

  1. UPDATE employee = > employee Table मे updation करने के लिए Table को Target किया गया है |
  2. SET E_name = 'Shyam' E_Name Column मे value 'Shyam' set करो |
  3. WHERE id = 1 Where id 1 है |
  4. con.commit() = > किए गए बदलावो को save करो |
  5. print("Name Updated Successfully!") = > Program के error free run हो जाने पर Name Updated Successfully! Show होगा |

Problem 6 :

company Manager नए Employee को account मे add करने से पहले check करता है कया वो company के काम के लायक है |
"employee" Table मे New Data insert/add करने के लिए नीचे एक Program दिया गया है , लेकिन Program को run करने पर Table मे data insert नही हो पा रहा है और data न insert होने का कारण साफ - साफ है इसलिए दिए गए Program को सही तरह से लिखे |

Code Explanation

  1. cur.execute(""" = > मतलब cursor execute करो |
  2. INSERT INTO employee (id, E_name, E_salary) = > employee Table के इन id, E_name, E_salary Columns मे data insert करो |
  3. VALUES (%s, %s, %s) = > Table के Columns मे VALUES %s के according add होगी |
  4. ("1", "Ram", 2000)) = > Table के Columns wise ये values add करो |
  5. con.commit() = > किए गए बदलावो को save करो |
  6. print("Data Inserted Successfully") = > Program के error free run हो जाने पर ये Data Inserted Successfully Show होगा |

Problem 7 :

किसी Employee के job छोड़ने पर Manager account से उस Employee का data delete कर देता है ताकि Last मे calculation करते Time कोई Problem ना हो |
एक ऐसा Python Program लिखे जो ,

  1. DataBase से "employee" Table को Target करे |
  2. "employee" Table के employee id 2 के data को delete कर दे |
  3. NOTE : Employee 2 के अलावा किसी और Employee का Data Delete न हो |

Code Explanation

  1. cur.execute(" = > मतलब लिखी गई queries को execute करो |
  2. DELETE FROM employee WHERE id = 2 = > employee Table से id 2 के Table मे stored data को delete करो |
  3. con.commit() = > Program मे किए गए Changes को save करो |
  4. print("Row Deleted Successfully!") = > Program के error free run हो जा ने प र Row Deleted Successfully! show होगा |

Problem 8 :

company के owner company के Employees को अब से Bouns भी देना चाहती है ताकि company के विकास मे Employees पहले से और बेहतर काम करे |
एक Python Program लिखे जो ,

  1. Databases से "employee" Table को target करे |
  2. "employee" Table मे एक E_Bonus नाम का coulumn add करे |

Code Explanation

  1. bonus = > bonus एक Variable है |
  2. "ALTER TABLE employee ADD COLUMN E_Bonus int AFTER E_salary" = > employee Table मे E_salary column के बाद E_Bonus Name का Column add करो |
  3. cur.execute(bonus) = > cursor bonus Variable को execute करो |
  4. con.commit() = > Program मे किए गए Changes को Database मे save करो |
  5. print("'E_Bonus' Column added ") = > Program के error free run हो जाने पर E_Bonus' Column added show होगा |

Problem 9 :

company मे मन चाही Opportunities न मिल पाने के कारण सभी Employees company को छोड़ देते है और फिर Manager सभी Employees का data delete कर देता है |
नीचे दिए गए Code editor मे एक Python Program लिखे जो ,

  1. सबसे पहले "employee" Table को Target करे |
  2. "employee" Table के सभी rows को एक साथ delete करे |
  3. NOTE : Table के सभी Rows Delete हो जाए लेकिन column नही |

Code Explanation

  1. cur.execute = > लिखी गई queries को execute करो |
  2. ("TRUNCATE TABLE employee") = > employee Table के सभी rows को delete कर दो
  3. con.commit() = > Program मे किए गए Changes को Database मे save करो |
  4. print("Row Deleted Successfully!") = > Program के error free run हो जाने पर All Rows Deleted Successfully! show होगा |

Problem 10 :

company के Close होते ही Manager सभी Files , Projects etc. को बंद कर देता है | एक Python Program लिखे जो ,

  1. Database की "employee" Table को Target करे |
  2. "employee" Table को Drop कर दे |

Code Explanation

  1. cur.execute = > cursor लिखी गई queries को execute करो |
  2. cur.execute("Drop TABLE employee") = > employee Table को close कर दो |
  3. con.commit() = > Program मे किए गए Changes को save करो |
  4. print("Table Closed") = > Program के error free run हो जाने पर Table Closed show होगा |

You have completed This Chapter ! 🎉

Now you can choose any one of the following options to test your knowledge :

💻

Code Practice

Solve practical exercises .

Practice Now
🏆

Knowledge Test

Answer the questions of the chapter .

Start Test
📘

Next Chapter

Continue your Python journey by reading the next chapter.

Next Chapter
🆘

Help Center

Users and we all help you together .

Peer Learning
Expert Advice
Live Chat
Fast Solutions