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