"""
对pymysql的封装,提供更实用的功能
"""
from pymysql import Connection
class MySqlHelper:
def __init__(self, database="", host="localhost", port=3306, user="root", password="123456"):
"""
创建一个数据库连接
:param database:str类型,数据库名称
:param host:str类型,数据库地址
:param port:int类型,端口号
:param user:str类型,用户名
:param password:str类型,密码
"""
self.conn = Connection(
host=host, port=port,
user=user, password=password,
database=database, autocommit=True
)
def fetch_one(self, sql, *params):
"""
执行查询语句,返回结果中的第一行
:param sql:str类型,需要执行的SQL语句
:param params:SQL语句需要的参数
:return:tuple类型,第一行
"""
try:
with self.conn.cursor() as cursor:
cursor.execute(sql, params)
return cursor.fetchone()
except Exception as e:
print(f"发生错误: {e}")
def fetch_all(self, sql, *params):
"""
执行查询语句,返回所有结果
:param sql:str类型,需要执行的SQL语句
:param params:SQL语句需要的参数
:return:tuple类型,所有查询结果
"""
try:
with self.conn.cursor() as cursor:
cursor.execute(sql, params)
return cursor.fetchall()
except Exception as e:
print(f"发生错误: {e}")
def fetch_yield(self, sql, *params):
"""
执行查询语句,通过生成器返回所有结果
:param sql:str类型,需要执行的SQL语句
:param params:SQL语句需要的参数
:return:生成器类型,推算每行数据
"""
try:
with self.conn.cursor() as cursor:
cursor.execute(sql, params)
while True:
data = cursor.fetchone()
if data is None:
break
yield data
except Exception as e:
print(f"发生错误: {e}")
def execute(self, sql, *params):
"""
执行单条增删改语句
:param sql:str类型,需要执行的SQL语句
:param params:SQL语句需要的参数
:return:tuple类型,(受影响行数,插入数据自增长主键)
"""
try:
with self.conn.cursor() as cursor:
return cursor.execute(sql, params), cursor.lastrowid
except Exception as e:
print(f"发生错误: {e}")
return ()
def execute_transaction(self, *sql_params):
"""
执行多条SQL语句,同时成功或同时失败
:param sql_params:将多个SQL与参数定义在多个元组中
:return:int类型,成功执行的语句数
"""
try:
self.conn.begin()
with self.conn.cursor() as cursor:
count = 0
for sql, params in sql_params:
count += cursor.execute(sql, params)
self.conn.commit()
return count
except Exception as e:
self.conn.rollback()
print(f"发生错误: {e}")
return -1
def execute_many(self, sql, *params):
"""
多次执行同一条SQL语句,每次使用序列中的参数来替换SQL语句中的占位符
:param sql:str类型,需要多次执行的SQL语句
:param params:tuple类型,每个SQL语句需要的参数也要作为tuple类型
:return:int类型,成功执行的语句数
"""
try:
with self.conn.cursor() as cursor:
count = cursor.executemany(sql, params)
return count
except Exception as e:
print(f"发生错误: {e}")
return -1
def execute_many_sql(self, *sql_params):
"""
执行多条SQL语句,返回失败的SQL语句
:param sql:str类型,需要多次执行的SQL语句
:param params:tuple类型,SQL语句需要的参数
:return:int类型,成功执行的语句数
"""
failed_sqls = []
with self.conn.cursor() as cursor:
for sql, params in sql_params:
try:
cursor.execute(sql, params)
except:
failed_sqls.append(sql)
return failed_sqls
def close(self):
"""
关闭数据库连接
"""
self.conn.close()
# 如果使用with语句,则需要实现__enter__和__exit__方法
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.close()
没有合适的资源?快使用搜索试试~ 我知道了~
python mvc架构的电影信息管理系统
![preview](https://csdnimg.cn/release/downloadcmsfe/public/img/white-bg.ca8570fa.png)
共11个文件
py:6个
pyc:5个
![preview-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/scale.ab9e0183.png)
需积分: 5 0 下载量 64 浏览量
2024-05-06
18:35:15
上传
评论
收藏 13KB ZIP 举报
温馨提示
mvc架构浅入,通过基于python的电影信息管理系统初步了解mvc架构
资源推荐
资源详情
资源评论
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
收起资源包目录
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
共 11 条
- 1
资源评论
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/c87ca858966c487493bd2cebe74f66d3_cerulea_n.jpg!1)
Jasper.w
- 粉丝: 2988
- 资源: 18
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)