Created
October 23, 2017 17:16
-
-
Save venkatsgithub1/8e381937ef70f9c97540ee103b7b94d7 to your computer and use it in GitHub Desktop.
Using Python to connect to MySQL database.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UseMySQL: | |
# Importing MySQLdb and as a smaller name for reusing it. | |
# To install MySQL Python connection use below command: | |
# pip install mysqlclient | |
import MySQLdb as _mysql | |
def __init__(self, host, user, password, db): | |
""" | |
Initializing MySQL details in init method. | |
Args: | |
self | |
host: Host name of the machine. | |
user: Username of mySQL DB. | |
password: Password of mySQL DB. | |
db: Database name of mySQL DB. | |
""" | |
self._host = host | |
self._user = user | |
self._password = password | |
self._db = db | |
def __enter__(self): | |
""" | |
This data model/dunder method is used | |
to employ auto open of my sql connection | |
and cursor inside 'with' and in turn | |
close them inside the same with the help | |
of exit method in order to avoid not so | |
closed database connections. | |
Args: | |
self | |
Returns: | |
self.cursor | |
""" | |
self._conn = self._mysql.connect( | |
host=self._host, user=self._user, passwd=self._password, db=self._db) | |
self.cursor = self._conn.cursor() | |
return self.cursor | |
def __exit__(self, exception_type, exception_value, traceback): | |
""" | |
This data model/dunder method is used to employ | |
auto close of connection and cursor as with block ends. | |
Args: | |
self | |
exception_type: Type of Exception. | |
exception_value: Exception Value. | |
traceback: Traceback value. | |
""" | |
self.cursor.close() | |
self._conn.close() | |
with UseMySQL(host="localhost", user="someuser", password="somepassword", db="sakila") as mySqlObj: | |
cursor = mySqlObj | |
cursor.execute("SELECT * FROM ACTOR") | |
for tup1 in cursor.fetchall(): | |
print(tup1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment