| | |
| | | # 把连接参数定义成字典 |
| | | import constant |
| | | |
| | | config = constant.MYSQL_CONFIG |
| | | |
| | | class Mysqldb: |
| | | # 初始化方法 |
| | | def __init__(self): |
| | | def __init__(self, config=constant.MYSQL_CONFIG): |
| | | self.config = config |
| | | # 初始化方法中调用连接数据库的方法 |
| | | self.conn = self.get_conn() |
| | | # 调用获取游标的方法 |
| | |
| | | # 连接数据库的方法 |
| | | def get_conn(self): |
| | | # **config代表不定长参数 |
| | | conn = pymysql.connect(**config) |
| | | conn = pymysql.connect(**self.config) |
| | | return conn |
| | | |
| | | # 获取游标 |
| | |
| | | # 插入单条数据 |
| | | mysqldb.execute("insert into clients(account,pwd,rule) values(%s,%s,%s)", ("test", 123456, "\"123")) |
| | | # 插入多条数据 |
| | | mysqldb.execute_many("insert into clients(account,pwd,rule) values(%s,%s,%s)", [("test", 123456, "\"123"),("test", 123456, "\"123")]) |
| | | mysqldb.execute_many("insert into clients(account,pwd,rule) values(%s,%s,%s)", |
| | | [("test", 123456, "\"123"), ("test", 123456, "\"123")]) |