当前位置: 首页 > news >正文

Python 如何操作数据库,让你使用 DeepSeek 开发数据库应用更加快 (Orm Bee)

Python 如何操作数据库,让你使用 DeepSeek 开发数据库应用更加快

操作数据库最好用 ORM 工具,可以提高开发效率.

ORM 就是实体与数据库表的映射,让我们可以用面向对象的方式来操作数据库.

简单易用,直接上代码.

使用Orm Bee操作数据库


class Orders:id = None  name = None remark = None# can ignoredef __repr__(self): return  str(self.__dict__)# also can use field type as :int        
class Orders8:__tablename__ = "orders"id:int = None  name:str = None remark:str = Nonedef __repr__(self): return  str(self.__dict__)class Student2:id = Nonename = None age = None  remark = Noneaddr = Nonedef __repr__(self): return  str(self.__dict__)from bee.api import Suid, SuidRich
from bee.config import PreConfig
from bee.honeyfactory import BF
from bee.osql.bee_enum import Opif __name__ == "__main__":# set bee.properties/bee.json config folderPreConfig.config_path="E:\\Bee-Project\\resources"# select recordsuid = Suid()orderList = suid.select(Orders())  # select all# insert    orders = Orders()orders.id = 1orders.name = "bee"orders.remark = "test"suid = Suid()suid.insert(orders)# update/deleteorders = Orders()orders.name = "bee130"# For safety reasons# Fields that are not present in the entity will be ignored.orders.ext = "aaa"  orders.id = 1suid = Suid()n1 = suid.update(orders)n2 = suid.delete(orders)print(n1)print(n2)# batch insertstudent0 = Student2()student0.name = "bee"student1 = Student2()student1.name = "bee1"student1.addr = ""student1.age = 40entity_list = []entity_list.append(student0)entity_list.append(student1)suidRich = SuidRich()insertNum = suidRich.insert_batch(entity_list)print(insertNum)#how to use Condition for advanced query and updatecondition = BF.condition()condition.op("age", Op.ge, 22)condition.op("remark", Op.eq, None)stuList = suidRich.select(Student2(), condition)# select ... from student2 where age >= ? and remark is nullfor stu in stuList:print(stu)# all stu'age add 1 if id>5condition = BF.condition()condition.setAdd("age", 1)condition.op("id", Op.ge, 5)updateNum = suidRich.updateBy(Student2(), condition)# update student2 set age = age + ? where id >= ?print("updateNum:", updateNum)#SuidRich: insert_batch,select_first,updateBy#复杂的where过滤条件、group,having,order by,Update Set等可使用Condition;

代码讲解:

实体: Orders, Orders8, Student2

PreConfig.config_path="E:\\Bee-Project\\resources"   是用来声明配置文件所在的文件夹.

  主要代码: 

    suid = Suid()
    #查询所有记录
    orderList = suid.select(Orders())  # select all
    #插入一条记录
    suid.insert(orders)
    #更新,删除记录
    n1 = suid.update(orders)
    n2 = suid.delete(orders)

还有可以通过 Condition 构造出更加复杂的 where 条件和 Update 记录.

 # select ... from student2 where age >= ? and remark is null

# update student2 set age = age + ? where id >= ?

其它功能

主要API在bee.api.py
Suid: simple API for Select/Update/Insert/Delete
SuidRich : select_paging, insert_batch, updateBy, select_first,select_by_id,
delete_by_id,select_fun,count,exist,create_table,index_normal,unique
PreparedSql: select, select_dict, modify, modify_dict

要连接数据库,要配置基本的用户名等连接信息:

配置db连接信息
1.1.can custom your db Module
in bee.json or bee.properties set dbModuleName{"dbname": "SQLite",  "database": "bee.db", //default support: pymysql,sqlite3,cx_Oracle,psycopg2 (no need set)"dbModuleName":"sqlite3"}
#value is: MySql,SQLite,Oracle,
#MySQL config
#bee.db.dbname=MySQL
#bee.db.host =localhost
#bee.db.user =root
#bee.db.password =
#bee.db.database =bee
#bee.db.port=3306# SQLite
bee.db.dbname=SQLite
bee.db.database =bee.db
1.2.if do not want to use the default config file(bee.json or bee.properties),
can set the db_config info yourself.# #mysqldict_config = {  'dbname':'MySQL','host': 'localhost',  # 数据库主机  'user': 'root',  # 替换为您的 MySQL 用户名  'password': '',  # 替换为您的 MySQL 密码  'database': 'bee',  # 替换为您的数据库名称  'port':3306}honeyConfig= HoneyConfig()honeyConfig.set_db_config_dict(dict_config)1.3.set connection directly:config = {  # 'dbname':'MySQL','host': 'localhost',  # 数据库主机  'user': 'root',  # 替换为您的 MySQL 用户名  'password': '',  # 替换为您的 MySQL 密码  'database': 'bee',  # 替换为您的数据库名称  'port':3306}honeyConfig= HoneyConfig()honeyConfig.set_dbname("MySQL")conn = pymysql.connect(**config)factory=BeeFactory()factory.set_connection(conn)

--------------------------------------------

快速开始:

安装依赖包

在命令行输入以下命令:

pip install ormbee

ORM Bee pypi url:
https://pypi.org/project/ormbee/

ORM Bee in Python!
Bee(BeePy)是Python版的ORM工具(还有Java版的).

Bee in Python url:
https://github.com/automvc/BeePy

Bee in Java url:
https://github.com/automvc/bee

http://www.xdnf.cn/news/195085.html

相关文章:

  • MySQL复合查询
  • HTML 从标签到动态效果的基础
  • react-navigation-draw抽屉导航
  • ArkTS基础实验 (二)
  • 数字人Live_Talking的搭建和使用
  • OpenResty深度解析:从卓伊凡的”隐形主流”论看其深度原理与应用生态-卓伊凡
  • 深入理解java线程池
  • stm32 阻塞式延时 与 非阻塞式延时
  • “数字驱动·智建未来——2025河北省建筑电气与智能化技术交流大会”
  • 【ACL系列论文写作指北14-科研心态与抗压管理】-走得远,比走得快更重要
  • 不同参数大小的DeepSeekR1模型对Java中new FileInputStream(“test.txt“).seek(100);语法错误的检查
  • 学习笔记:Qlib 量化投资平台框架 — MAIN COMPONENTS (Part I)
  • XrayR启动失败
  • 架构进阶:详解108页系统架构设计与详细设计知识讲座【附全文阅读】
  • 品融电商:全域电商代运营的领航者,驱动品牌长效增长
  • 第四章:Messaging and Memory
  • C语言中的指针详解
  • RSS‘25|CMU提出统一空中操作框架:以末端执行器为中心,无人机实现高精度遥操作
  • Cursor + Figma-Context-MCP ,让 Cursor 获取 Figma 设计图信息,实现 AI 生成页面的高度还原
  • 力扣面试150题--K 个一组翻转链表
  • 机器人--激光雷达
  • ESG跨境电商怎么样?esg跨境电商有哪些功用?
  • 阅读MySQL实战45讲第11天
  • uniapp打包apk如何实现版本更新
  • Spring MVC异常处理利器:深入理解HandlerExceptionResolver
  • SpringBoot实现接口防刷的5种高效方案详解
  • C#/.NET/.NET Core技术前沿周刊 | 第 36 期(2025年4.21-4.27)
  • AudioSet 音频中文类别
  • 蚂蚁seo蜘蛛池:提升网站收录的秘密武器
  • Nacos源码—1.Nacos服务注册发现分析二