# -*- coding: utf-8 -*-
import mysql.connector
__all__ = []
class CommonMysql:
def __init__(self):
config = {
'user': 'user',
'password': 'password',
'host': 'host',
'db': 'meituanex',
'port': 3306,
'raise_on_warnings': True
}
try:
self.cnn = mysql.connector.connect(**config)
self.cursor = self.cnn.cursor()
except mysql.connector.Error as e:
print(('Connect fails!{}'.format(e)))
raise e
def execute(self, sql):
"""
@summary: Execute SQL statement
@param sql:
@return:
"""
self.cursor.execute(sql)
result = []
for i in self.cursor:
result.append(i)
self.cnn.commit()
return result
def __exit__(self, exc_type, exc_val, exc_tb):
self.cursor.close()
self.cnn.close()
def close(self):
self.cursor.close()
self.cnn.close()
from sqltools.CommonMysql import CommonMysql
class OperationDb(CommonMysql):
# def __int__(self, db):
# CommonMysql.__init__(self, db=db)
def get_all_interface_name(self):
sql = "select xx from xx WHERE id={0}".format(xx)
data = self.execute(sql)
return data
如果有字符串,用 ' ' ,如下所示,传的job_name是一个字符串,如果不用'{0}',直接用{0},会报错
sql = "update unit_case_result_job_msg set job_name='{0}', job_number={1} WHERE id=1".format(job_name, job_number)