1 Star 0 Fork 0

吕小布/ymsd

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Database.py 795 Bytes
一键复制 编辑 原始数据 按行查看 历史
吕小布 提交于 2024-08-19 09:03 . 首次提交
from peewee import *
database = MySQLDatabase('db_dev_12280', user='money111', password='money_770', host='8.140.162.237', port=3307)
# 数据模型
class BaseModel(Model):
class Meta:
database = database
class DatabaseConnection:
def __enter__(self):
# 建立数据库连接
database.connect()
return database
def __exit__(self, exc_type, exc_val, exc_tb):
# 关闭数据库连接
database.close()
# 数据表结构
class Test(BaseModel):
id = AutoField()
name = CharField(unique=True)
age = IntegerField()
# demo
def demo():
with DatabaseConnection():
# 查询所有
for user in Test.select():
print(f"ID: {user.id}, Name: {user.name}, Age: {user.age}")
print("查询完成")
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/lvyinghaowanglu/ymsd.git
git@gitee.com:lvyinghaowanglu/ymsd.git
lvyinghaowanglu
ymsd
ymsd
master

搜索帮助