Python कई सारे Databases को support करती है data को store , fetch और maintain करने के लिए , इस topic में आप Python के साथ MySQL Database पढ़ेंगे।

What is MySQL ?

MySQL एक Open Source RDBMS ( Relational Database Management System ) Software है जो Digital Data store और Manage करने के लिए use किया जाता है । MySQL का use कई सारी programming languages like : PHP , Java , Python , PERL , .NET के साथ use करके Web and Desktop Application में भी किया जाता है।


ज्यादा जानने के लिए आप हमारे MySQL Tutorials पढ़ सकते हैं।


Python से MySQL Database handle करने के लिए आपको दो कई चीज़ें चाहिए : पहला MySQL Database , दूसरा MySQL Driver.

Install MySQL Database

अगर आपके system में Operating System Window है तो MySQL की official website https://dev.mysql.com/downloads/installer पर जाकर stable version download करके install कर सकते हैं।
Or Read : Install MySQL On Window.

Python Install MySQL Driver

MySQL Database को access करने के लिए आपको MySQL Driver install करना पड़ेगा। PIP के through आप easily MySQL Driver install कर सकते हैं।

PIP Python में पहले से ही installed होता है , अब MySQL Driver install करने के लिए simply नीचे दी गयी command run करें।

python -m pip install mysql-connector-python

that's it . बस ये command run करने से MySQL driver install हो जाएगा।

Test MySQL Connector

import mysql.connector

MySQL Connector install हुआ है या नहीं यह जानने के लिए ऊपर दिए गए code को run करें , अगर कोई error नहीं आये तो MySQL Connector successfully install हो गया है और उसे use कर सकते हैं। और अगर कोई error आये तो समझ लेना install नहीं हुआ है।

Python Connect MySQL

Database connect करने के लिए सबसे पहले तो mysql module को import करना पड़ेगा। फिर mysql module में defined connector class में connect() method का use करके database connect करते हैं।

connect() function 4 arguments accept करता है जो इस प्रकार हैं -

mydb = myconn.connect(
  host = 'localhost',
  user = 'root',
  password = 'passowrd',
  database = 'database_name'

)

  1. host | required : host name , mostly इसकी value localhost हो होती है।

  2. user | required : यह database user name है।

  3. passowrd | required : यह database password है।

  4. databse | optional : यह database name है। हालाँकि यह optional है।


Copy Fullscreen Close Fullscreen Run
# make alias name of mysql connector.
#So that you do not have to write such a long name again and again. 
import mysql.connector as myconn
""" Here -
1. mysql is a module.
2. connector is a class defined in mysql module.
3. and connect() is a method defined in connector class.
"""
mydb = myconn.connect(
  host = 'localhost',
  user = 'root',
  password = ''
)

print(mydb)
Output
C:\Users\Rahulkumar\Desktop\python>python connect_mysql.py
<mysql.connector.connection.MySQLConnection object at 0x0089E730>

ऊपर दिया गया example run करने पर अगर output कुछ ऐसा दिखे तो database connect हो चुका है।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers