Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def __init__(self, my_user, my_password, my_port, my_host, my_database):
- self.user = my_user
- self.password = my_password
- self.host = my_host
- self.port = my_port
- self.database = my_database
- def open_connection(self):
- try:
- #https://dev.mysql.com/doc/connector-python/en/connector-python-connectargs.html
- self._myconnection = MySQLConnection(user=self.user, password=self.password, port=self.port, host=self.host,
- database=self.database)
- if self._myconnection.is_connected():
- print("Connection Is Established")
- return self._myconnection
- else:
- print("Connection Is Not Established")
- return self._myconnection
- except mysql.connector.Error as err:
- if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
- print("Something is wrong with your user name or password")
- elif err.errno == errorcode.ER_BAD_DB_ERROR:
- print("Database does not exist")
- else:
- print("Error not defined")
- print(err.msg)
- def get_connection(self):
- return self._myconnection
- def close_connection(self):
- if self._myconnection==None:
- print("Connection is null")
- if self._myconnection.is_connected:
- print("Trying to close connection")
- print(self._myconnection)
- self._myconnection.close()
- if not self._myconnection.is_connected:
- print("Connection is closed")
- if self._myconnection.is_connected:
- print("Connection is opened")
- import MySqlConnection_Helper.Connector
- s = MySqlConnection_Helper.Connector.MyConnector("username", "password", "port", "127.0.0.1", "database")
- s.open_connection()
- s.close_connection()
Add Comment
Please, Sign In to add comment