Advertisement
Guest User

Untitled

a guest
Nov 10th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. class DBConnection(object):
  2. def __init__(self, driver, serve,
  3. database, user, password):
  4.  
  5. self.driver = driver
  6. self.server = server
  7. self.database = database
  8. self.user = user
  9. self.password = password
  10.  
  11. def __enter__(self):
  12. self.dbconn = pyodbc.connect("DRIVER={};".format(self.driver) +
  13. "SERVER={};".format(self.server) +
  14. "DATABASE={};".format(self.database) +
  15. "UID={};".format(self.user) +
  16. "PWD={};".format(self.password) +
  17. "CHARSET=UTF8",
  18. # "",
  19. ansi=True)
  20.  
  21. return self.dbconn
  22.  
  23. def __exit__(self, exc_type, exc_val, exc_tb):
  24. self.dbconn.close()
  25.  
  26. def get_database_connection():
  27. conn = DBConnection.connection
  28. if not conn:
  29. conn = DBConnection.connection = DBConnection.create_connection()
  30. return conn
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement