Created
September 17, 2018 21:56
-
-
Save RyadPasha/b42543428b2f0dce01715901d8ef57af to your computer and use it in GitHub Desktop.
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
""" | |
Oracle Database Connection Wrapper. | |
@author: Mohamed Riyad <@RyadPasha> | |
@url: http://ryadpasha.com | |
@email: me@ryadpasha.com | |
@license: MIT License | |
""" | |
import cx_Oracle | |
class OracleDB: | |
""" | |
Usage: | |
db = OracleDB("user", "pass", "yourserver.com", 1523, "YOUR_SID") | |
db.connect() | |
db.cursor.execute("SELECT yourcolumn FROM yourtable") | |
result1 = [x[0] for x in db.cursor] | |
db.close() | |
db.connect() | |
db.cursor.execute("SELECT yourothercolumn FROM yourothertable") | |
result2 = [x[0] for x in db.cursor] | |
db.close() | |
# do stuff with result1 and result2 ... | |
""" | |
def __init__(self, user, password, server, port, sid): | |
self.tns = cx_Oracle.makedsn(server, port, sid) | |
self.connection = None | |
self.cursor = None | |
self.user = user | |
self.password = password | |
def connect(self): | |
self.connection = cx_Oracle.connect(self.user, self.password, self.tns) | |
self.cursor = self.connection.cursor() | |
def close(self): | |
self.cursor.close() | |
self.connection.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment