python peewee
from peewee import MySQLDatabase, Model
from peewee import CharField, IntegerField, BooleanField
# 引入 随机数据包
import random
from faker import Faker
fake = Faker('zh_CN')
# 创建 mysql 连接关系
mysql_db = MySQLDatabase("peewee", user="root", host="127.0.0.1", port=3306, passwd="123456", charset='utf8')
'''
需要手动添加 mysql 的驱动库,不是光添加一个 MySQLDatabase 就能行的
pip install pymysql
'''
# 创建链接基类
class BaseModel(Model):
class Meta:
database = mysql_db
class User(BaseModel):
username = CharField() # 名字为 varchar 类型
age = IntegerField() # 年龄为 int 类型
is_graduate = BooleanField() # 是否毕业,bool 类型
class Meta:
db_table = "test_user"
def create_date():
users = []
for i in range(20):
age = random.randint(1, 82)
users.append({"username": fake.name(), "age": age, "is_graduate": age > 22})
with mysql_db.atomic():
query = User.insert_many(users)
query.execute()
if __name__ == "__main__":
first = True
if first:
User.create_table() # 创建表
else:
User.drop_table() # 删除表
# create_date()
user_01 = User.select().where(User.id == "1").get()
print(user_01.username)
print(type(user_01))
参考文献
https://geek-docs.com/python/python-tutorial/python-peewee.html#ftoc-heading-9
https://docs.peewee-orm.com/en/latest/peewee/query_operators.html
python peewee的更多相关文章
- [Python]peewee使用经验
peewee 使用经验 本文使用案例是基于 python2.7 实现 以下内容均为个人使用 peewee 的经验和遇到的坑,不会涉及过多的基本操作.所以,没有使用过 peewee,可以先阅读文档 正确 ...
- [Python]peewee 使用经验
peewee 使用经验 本文使用案例是基于 python2.7 实现 以下内容均为个人使用 peewee 的经验和遇到的坑,不会涉及过多的基本操作.所以,没有使用过 peewee,可以先阅读文档 正确 ...
- python -- peewee处理数据库连接
目前,实现了的Database子类有三个:SqliteDatabase.MySQLDatabase.PostgresqlDatabase class SqliteDatabase(Database) ...
- python peewee.ImproperlyConfigured: MySQLdb or PyMySQL must be installed.
最近在学习Python,打算先看两个在线教程,再在github上找几个开源的项目练习一下,在学到“被解放的姜戈”时遇到django同步数据库时无法执行的错误,记录一下. 错误现象: 执行python ...
- 使用 Python 的 SQLite JSON1 和 FTS5 扩展
早在九月份,编程界出现一个名为 json1.c 的文件,此前这个文件一直在 SQLite 的库里面.还有,笔者也曾总结通过使用新的 json1 扩展来编译 pysqlite 的技巧.但现在随着 SQL ...
- Python ORM框架之 Peewee入门
之前在学Django时,发现它的模型层非常好用,把对数据库的操作映射成对类.对象的操作,避免了我们直接写在Web项目中SQL语句,当时想,如果这个模型层可以独立出来使用就好了,那我们平台操作数据库也可 ...
- Python:轻量级 ORM 框架 peewee 用法详解(二)——增删改查
说明:peewee 中有很多方法是延时执行的,需要调用 execute() 方法使其执行.下文中不再特意说明这个问题,大家看代码. 本文中代码样例所使用的 Person 模型如下: class Per ...
- python轻量级orm框架 peewee常用功能速查
peewee常用功能速查 peewee 简介 Peewee是一种简单而小的ORM.它有很少的(但富有表现力的)概念,使它易于学习和直观的使用. 常见orm数据库框架 Django ORM peewee ...
- Python’s SQLAlchemy vs Other ORMs[转发 4]peewee
peewee peewee is a small, expressive ORM. Compared to other ORMs, peewee focuses on the principal of ...
- Python:用 peewee 框架连接 SQL Server
Peewee 默认支持 Sqlite.MySQL.PostgreSQL 三种数据库,如果要使用其他数据库,需要同时安装扩展库.比如 SQL Server,需要安装 peewee-mssql. 但是安装 ...
随机推荐
- 视觉十四讲:第九讲_BA优化_g2o
1.投影模型和BA代价函数 这个流程就是观测方程 之前抽象的记为: \(z = h(x, y)\) 现在给出具体的参数话过程,x指此时相机的位姿R,t,它对应的李代数为\(\xi\).路标y即为这里的 ...
- 微信小程序分包
当我们程序太大的时候,打开小程序就会比较慢,此处就需要用到分包加载,按照模块划分不同的包,让用户在需要的时候才加载对用的模块,也就是用户在进入某些页面的时候才下载该页面的资源,提高小程序的打开速度,以 ...
- Cesium JulianDate(十八)
代表天文朱利安日期,它是自4712年1月1日(公元前4713年)正午以来的天数.为了提高精度,该类存储的日期部分和秒数部分是分开的.并且为了算术安全和表示闰秒,该日期始终存储在国际原子时间标准中 (T ...
- WPF BasedOn 自定义样式 例:ComboBox 组合框
自定义样式 ComboBox 组合框 <Window.Resources> <Style x:Key="ComboBox01" TargetType=" ...
- 由于pom文件中依赖了redis,服务中没用到,微服务健康监控会报redis的错误
在启动类里加 @SpringBootApplication(exclude = RedisAutoConfiguration.class)@MapperScan("com.news.rept ...
- python 2.7中matplotlib的所有版本
matplotlib versions: 0.86, 0.86.1, 0.86.2, 0.91.0, 0.91.1, 1.0.1, 1.1.0, 1.1.1, 1.2.0, 1.2.1, 1.3.0, ...
- Educational Codeforces Round 137 (Rated for Div. 2) - E. FTL
DP Problem - E - Codeforces 题意 有个 BOSS 有 \(H\;(1<=H<=5000)\) 血量,\(s\) 点防御 有两种武器可用攻击 BOSS,伤害分别为 ...
- stm32 出入栈
Start.S 一般指定栈顶指针及栈大小 1.硬件中断 有硬件入栈和软件入栈部分 硬件入栈寄存器: R0,R1,R2,R3,R12,PSR 软件入栈寄存器: r4 - r11 2.程序切换入栈 ...
- 狂神--Docker
官网地址 官网 https://www.docker.com/ 官方文档Docker文档是超详细的 https://docs.docker.com/ 仓库地址 https://hub.docker.c ...
- DEV 导出多行头
//在winfrom 中添加个Gridcontrol 和按钮 public partial class MultTitle : XtraForm { BandedGridView bandedGrid ...