Guest User

Untitled

a guest
Nov 2nd, 2018
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. def __init__(self, my_user, my_password, my_port, my_host, my_database):
  2. self.user = my_user
  3. self.password = my_password
  4. self.host = my_host
  5. self.port = my_port
  6. self.database = my_database
  7.  
  8. def open_connection(self):
  9.  
  10. try:
  11. #https://dev.mysql.com/doc/connector-python/en/connector-python-connectargs.html
  12. self._myconnection = MySQLConnection(user=self.user, password=self.password, port=self.port, host=self.host,
  13. database=self.database)
  14. if self._myconnection.is_connected():
  15. print("Connection Is Established")
  16.  
  17. return self._myconnection
  18. else:
  19. print("Connection Is Not Established")
  20. return self._myconnection
  21.  
  22. except mysql.connector.Error as err:
  23. if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
  24. print("Something is wrong with your user name or password")
  25. elif err.errno == errorcode.ER_BAD_DB_ERROR:
  26. print("Database does not exist")
  27. else:
  28. print("Error not defined")
  29. print(err.msg)
  30.  
  31. def get_connection(self):
  32. return self._myconnection
  33.  
  34. def close_connection(self):
  35.  
  36. if self._myconnection==None:
  37. print("Connection is null")
  38. if self._myconnection.is_connected:
  39. print("Trying to close connection")
  40. print(self._myconnection)
  41.  
  42. self._myconnection.close()
  43.  
  44. if not self._myconnection.is_connected:
  45. print("Connection is closed")
  46.  
  47. if self._myconnection.is_connected:
  48. print("Connection is opened")
  49.  
  50. import MySqlConnection_Helper.Connector
  51.  
  52. s = MySqlConnection_Helper.Connector.MyConnector("username", "password", "port", "127.0.0.1", "database")
  53. s.open_connection()
  54. s.close_connection()
Add Comment
Please, Sign In to add comment