python操作mysql数据库
import os
import pymysql
from common.myConf import MyConf
from common.my_path import conf_dir
class MyMysql:
def __init__(self):
# 实例化配置类对象
conf = MyConf(os.path.join(conf_dir, "mysql.ini"))
# 连接数据库/生成游标
self.db = pymysql.connect(
host=conf.get("mysql", "host"),
user=conf.get("mysql", "user"),
password=conf.get("mysql", "passwd"),
port=conf.getint("mysql", "port"),
database=conf.get("mysql", "database"),
charset="utf8",
cursorclass=pymysql.cursors.DictCursor
)
# 2、创建游标
self.cur = self.db.cursor()
def get_count(self,sql):
count = self.cur.execute(sql)
return count
def get_one_data(self,sql):
self.cur.execute(sql)
return self.cur.fetchone()
def get_many_data(self,sql, size=None):
self.cur.execute(sql)
if size:
return self.cur.fetchmany(size)
else:
return self.cur.fetchall()
# def update_data(self):
# 事务
# 提交commit 回滚 rollback
# pass
def close_conn(self):
self.cur.close()
self.db.close()
if __name__ == '__main__':
conn = MyMysql()
sql = "select id from member where mobile_phone='18420100022'"
count = conn.get_count(sql)
print('count:',count)
# conn.close_conn()
# sql = "select member.leave_amount from member where id=16"
# res = conn.get_one_data(sql)
# print(res)
![在这里插入图片描述](https://img-blog.csdnimg.cn/6b2881f0b3324bfb8c06a201747001d6.png)