当前位置: 首页 > 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

相关文章:

  • 相机-IMU联合标定:相机标定
  • 大模型(LLMs)加速篇
  • Improving Deep Learning For Airbnb Search
  • 9.学习笔记-springboot(P90-P104)
  • OSCP - Proving Grounds - Wpwn
  • 同步时钟与异步时钟
  • Python 实现的运筹优化系统数学建模详解(动态规划模型)
  • qemu(3) -- qemu-user使用
  • 【Machine Learning Q and AI 读书笔记】- 01 嵌入、潜空间和表征
  • 4.环境变量
  • 对Electron打包的exe文件进行反解析
  • 中级社会工作者工作内容有哪些
  • 【go】go语言slice/map的产生背景,及原理理解
  • 【解决方案】Linux解决CUDA安装过程中GCC版本不兼容
  • LLaMA-Factory部署以及大模型的训练(细节+新手向)
  • C语言高频面试题——局部变量和全局变量可以重名吗?
  • 02《小地图实时》Unity
  • 区块链随学随记
  • 第二章 信息技术发展(2.2 新一代信息技术及应用)
  • PostgreSQL无法查看表中数据问题排查
  • 大理杨徐邱再审上诉案宣判:驳回上诉,维持再审一审判决
  • 黄晓丹:用“诗心”找到生存的意义
  • 挤占学生伙食费、公务考察到景区旅游……青岛通报5起违规典型问题
  • 商务部:4月份以来的出口总体延续平稳增长态势
  • 加拿大警方:已确认有9人在温哥华驾车撞人事件中遇难
  • 铁路上海站五一假期预计发送446万人次,同比增长8.4%