insert DB
def insert_to_database(self, result): import pymysql import time cur_time = time.strftime("%D %H:%M:%S", time.localtime(time.time())) db = pymysql.connect( host="10.1xx.xx.xx", port=3xx, user="xxx", password="xx", database="test_xx", ) cursor = db.cursor() if self.testcase["xxxx"] == "mm": version = branch = "" result = { "Date": cur_time, "CaseID": self.testcase["id"], "aaa": self.testcase["aa"], "bbb": self.testcase["bbb"], "ccc": self.testcase["ccc"], "eee": self.testcase["card_type"], "Version": version, "Branch": branch, } result_keys = ",".join(result.keys()) result_values = ",".join(["%s"] * len(result)) insert_sql = "INSERT INTO database_xyz ({result_keys}) VALUES ({result_values})".format( result_keys=result_keys, result_values=result_values ) try: cursor.execute(insert_sql, tuple(result.values())) print("INSERT Successful") db.commit() except: print("INSERT Failed!") db.rollback() cursor.close() db.close()