# -*-coding: Utf-8 -*-
# @File : date .py
# author: 石明亮
# Time:2024/1/31
import pymysql
class Database():
# **db是指连接数据库时需要的参数
# 初始化时就连接数据库
def __init__(self, **db):
try:
# 连接数据库的参数我不希望别人可以动,所以设置私有
self.__conn = pymysql.connect(**db)
self.__cursor = self.__conn.cursor()
except Exception as e:
print("数据库连接失败:\n", e)
# 查询一条数据
# 参数:表名table_name,条件factor_str,要查询的字段field 默认是查询所有字段*
def select_one(self, table_name, factor_str='', field="*"):
if factor_str == '':
sql = f"select {field} from {table_name}"
else:
sql = f"select {field} from {table_name} where {factor_str}"
self.__cursor.execute(sql)
return self.__cursor.fetchone()
# 查询多条数据
# 参数:要查询数据的条数num,表名table_name,条件factor_str,要查询的字段field 默认是查询所有字段*
def select_many(self, num, table_name, factor_str='', field="*"):
if factor_str == '':
sql = f"select {field} from {table_name}"
else:
sql = f"select {field} from {table_name} where {factor_str}"
self.__cursor.execute(sql)
return self.__cursor.fetchmany(num)
# 查询全部数据
# 参数:表名table_name,条件factor_str,要查询的字段field 默认是查询所有字段*
def select_all(self, table_name, factor_str='', field="*"):
if factor_str == '':
sql = f"select {field} from {table_name}"
else:
sql = f"select {field} from {table_name} where {factor_str}"
self.__cursor.execute(sql)
return self.__cursor.fetchall()
# 新增数据
def insert(self,table_name, value):
sql = f"insert into {table_name} values {value}"
try:
self.__cursor.execute(sql)
self.__conn.commit()
print("插入成功")
except Exception as e:
print("插入失败\n", e)
self.__conn.rollback()
# 修改数据
# 参数:表名,set值(可能是一个,也可能是多个,所以用字典),条件
def update(self, table_name, val_obl,change_str):
sql = f"update {table_name} set"
# set后面应该是要修改的字段,但是可能会修改多个字段的值,所以遍历一下
# key对应字段的名,val对应字段的值
for key, val in val_obl.items():
sql += f" {key} = {val},"
# 遍历完的最后面会有一个逗号,所以给它切掉,然后再拼接条件
# !!!空格很重要
sql = sql[:-1]+" where "+change_str
try:
self.__cursor.execute(sql)
self.__conn.commit()
print("修改成功")
except Exception as e:
print("修改失败\n", e)
self.__conn.rollback()
# 删除数据
def delete(self,table_name, item):
sql = f"delete from {table_name} where {item}"
try:
self.__cursor.execute(sql)
self.__conn.commit()
print("删除成功")
except Exception as e:
print("删除失败\n", e)
self.__conn.rollback()
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
mysql工具类.zip (6个子文件)
mysql工具类
封装一个需要自己写SQL语句的mysql工具类
data_one.py 2KB
run.py 1KB
__pycache__
data_one.cpython-311.pyc 3KB
封装一个不用写SQL语句,只需要填参数的工具类
over.py 1KB
__pycache__
date.cpython-311.pyc 5KB
date.py 3KB
共 6 条
- 1
资源评论
石明亮(HRJT)
- 粉丝: 150
- 资源: 8
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功