Best Python code snippet using autotest_python
mysql.py
Source:mysql.py
...14 Creates a new instance15 '''16 super(MySQLDatabaseManager, self).__init__(name, admin, admin_password,17 user, password, host)18 if self.admin_credentials_valid():19 self.admin_connection = MySQLdb.connect(host=self.host,20 user=self.admin,21 passwd=self.admin_password)22 else:23 self.admin_connection = None24 logging.error("Failed to logon as the database admin user")25 def run_sql(self, sql):26 '''27 Runs the given SQL code blindly28 '''29 self.admin_connection.select_db(self.name)30 cursor = self.admin_connection.cursor()31 try:32 cursor.execute(sql)33 except (MySQLdb.ProgrammingError, MySQLdb.OperationalError):34 self.admin_connection.rollback()35 return False36 return True37 def exists(self):38 '''39 Checks if the database instance exists40 '''41 try:42 MySQLdb.connect(user=self.admin,43 passwd=self.admin_password,44 db=self.name,45 host=self.host)46 return True47 except MySQLdb.OperationalError:48 return False49 def admin_credentials_valid(self):50 '''51 Checks if the admin user credentials are valid system wide52 What this means is that we won't try to connect to a specific database53 instance, but to the RDBMS as a whole (where appropriate)54 '''55 try:56 MySQLdb.connect(host=self.host,57 user=self.admin,58 passwd=self.admin_password)59 return True60 except MySQLdb.OperationalError:61 return False62 def create_instance(self):63 '''...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!