Python轻量级ORM框架Peewee访问sqlite数据库的方法详解

作者:不想长大啊 时间:2024-01-23 06:47:45 

本文实例讲述了Python轻量级ORM框架Peewee访问sqlite数据库的方法。分享给大家供大家参考,具体如下:

ORM框架就是 object relation model,对象关系模型,用来实现把数据库中的表 映射到 面向对象编程语言中的类,不需要写sql,通过操作对象就能实现 增删改查。

ORM的基本技术有3种:

(1)映射技术

数据类型映射:就是把数据库中的数据类型,映射到编程语言中的数据类型。比如,把数据库的int类型映射到Python中的integer 类型。
类映射:把数据库中的表,映射到面向对象编程语言的类,这样就不用写sql,直接操作对象就可以了。
关系映射:关系型数据库最大的特点在于实体之间的关系,也就是表之间通过主外键的设置,产生的关联,把这种关联映射成编程语言中基于对象引用的关系连接。

(2)CURD技术

CURD就是增加、更新、检索、删除的意思,就是实现了数据库中的增删改查的功能。

(3)缓存技术

把数据库中查询到的数据,以类对象的形式,存储在内存中,用的时候随时提取。

在ORM查询命令中,并不会去查询数据库,而是当真正要读取数据时,才到数据库中去查数据。

一、安装peewee


c:\Python27\Scripts>pip install peewee
Collecting peewee
Downloading peewee-2.8.5.tar.gz (506kB)
 100% |████████████████████████████████| 512kB 437kB/s
Installing collected packages: peewee
Running setup.py install for peewee ... done
Successfully installed peewee-2.8.5

二、定义表到类的映射

先把表定义为类,保存为 orm.py,后面的代码会引用这个模块。

代码如下:


# -*- coding:utf8 -*-
from peewee import *
db = SqliteDatabase('test.db')
class BaseModel(Model):
 class Meta:
   database = db
class Course(BaseModel):
 id = PrimaryKeyField()
 title = CharField(null = False)
 period = IntegerField()
 description = CharField()
 class Meta:
   order_by = ('title',)
   db_table = 'course'
class Teacher(BaseModel):
 id = PrimaryKeyField()
 name = CharField(null = False)
 gender = BooleanField()
 address = CharField()
 course_id = ForeignKeyField(Course,to_field='id',related_name = "course")
 class Meta:
   order_by = ('name',)
   db_table = 'teacher'

三、创建表、新增记录


# -*- coding:utf8 -*-
from orm import *
Course.create_table()
Teacher.create_table()
Course.create(id = 1,title='经济学',period = 320,description='文科必修')
Course.create(id = 2,title='大学语文',period = 300,description='所有学科必修')
Course.create(id = 3,title='操作系统',period = 320,description='计算机必修')
Course.create(id = 4,title='马克思主义哲学',period = 320,description='必修')
Teacher.create(id = 1,name = '张三',gender=True,address='...',course_id = 1)
Teacher.create(id = 2,name = '李四',gender=False,address='-',course_id = 2)
Teacher.create(id = 3,name = '王五',gender=True,address='=',course_id = 3)

四、更复杂的操作


# -*- coding:gbk -*-
from orm import *
#获取1行数据
record = Course.get(Course.title=='大学语文')
print("课程:%s ,学时: %d" %(record.title,record.period))
#更新这行数据的period字段,保存
record.period = 200
record.save()
print("学分改为:%d" % record.period)
#获取1行数据,再删除,如果不存在,则会报错
record = Course.get(Course.title=='马克思主义哲学')
record.delete_instance()
###查询所有数据
course = Course.select()
###查询符合条件的数据,排序
course = Course.select().where(Course.id <3).order_by(Course.period.desc())
###计算平均值
total = Course.select(fn.Avg(Course.period)).alias('avg_period')
###更新数据
Course.update(period=300).where(Course.id>2).execute()
###关联数据
Record = Course.select().join(Teacher).where(Teacher.gender == True)

操作完成后,可以用sqlite3 test.db ,然后用命令查看表数据。

希望本文所述对大家Python程序设计有所帮助。

标签:Python,ORM,sqlite
0
投稿

猜你喜欢

  • PHP中PDO基础教程 入门级

    2023-11-14 16:34:39
  • Python利用matplotlib.pyplot.boxplot()绘制箱型图实例代码

    2022-11-18 05:08:26
  • 一文带你掌握Python中多线程和线程池的使用方法

    2022-10-20 21:53:09
  • SQLServer中的触发器基本语法与作用

    2024-01-25 18:18:02
  • 不同版本中Python matplotlib.pyplot.draw()界面绘制异常问题的解决

    2021-04-23 03:41:16
  • Python脚本如何在bilibili中查找弹幕发送者

    2021-05-18 21:46:15
  • 几个小技巧帮你实现Golang永久阻塞

    2024-02-14 10:37:29
  • python中watchdog文件监控与检测上传功能

    2022-03-17 13:46:58
  • Python中的字符串操作和编码Unicode详解

    2021-02-24 18:37:11
  • Mysql中正则表达式Regexp常见用法及说明

    2024-01-14 21:51:35
  • python中的decorator的作用详解

    2021-05-24 16:44:40
  • 一行代码让 Python 的运行速度提高100倍

    2021-05-18 12:29:50
  • 使用OpenCV circle函数图像上画圆的示例代码

    2021-03-12 17:30:41
  • python3.9和pycharm的安装教程并创建简单项目的步骤

    2021-06-18 05:34:16
  • 6行的js上下滑动广告效果

    2008-11-27 12:26:00
  • 分享python 写 csv 文件的两种方法

    2023-04-07 07:03:47
  • Go项目编写Makefile规则文件概述

    2024-02-06 01:20:07
  • vue2.0+vue-dplayer实现hls播放的示例

    2024-05-29 22:46:56
  • asp如何制作一个搜索引擎链接程序?

    2010-07-07 12:26:00
  • python 动态生成变量名以及动态获取变量的变量名方法

    2021-05-18 10:28:20
  • asp之家 网络编程 m.aspxhome.com