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的更多相关文章

  1. [Python]peewee使用经验

    peewee 使用经验 本文使用案例是基于 python2.7 实现 以下内容均为个人使用 peewee 的经验和遇到的坑,不会涉及过多的基本操作.所以,没有使用过 peewee,可以先阅读文档 正确 ...

  2. [Python]peewee 使用经验

    peewee 使用经验 本文使用案例是基于 python2.7 实现 以下内容均为个人使用 peewee 的经验和遇到的坑,不会涉及过多的基本操作.所以,没有使用过 peewee,可以先阅读文档 正确 ...

  3. python -- peewee处理数据库连接

    目前,实现了的Database子类有三个:SqliteDatabase.MySQLDatabase.PostgresqlDatabase class SqliteDatabase(Database) ...

  4. python peewee.ImproperlyConfigured: MySQLdb or PyMySQL must be installed.

    最近在学习Python,打算先看两个在线教程,再在github上找几个开源的项目练习一下,在学到“被解放的姜戈”时遇到django同步数据库时无法执行的错误,记录一下. 错误现象: 执行python ...

  5. 使用 Python 的 SQLite JSON1 和 FTS5 扩展

    早在九月份,编程界出现一个名为 json1.c 的文件,此前这个文件一直在 SQLite 的库里面.还有,笔者也曾总结通过使用新的 json1 扩展来编译 pysqlite 的技巧.但现在随着 SQL ...

  6. Python ORM框架之 Peewee入门

    之前在学Django时,发现它的模型层非常好用,把对数据库的操作映射成对类.对象的操作,避免了我们直接写在Web项目中SQL语句,当时想,如果这个模型层可以独立出来使用就好了,那我们平台操作数据库也可 ...

  7. Python:轻量级 ORM 框架 peewee 用法详解(二)——增删改查

    说明:peewee 中有很多方法是延时执行的,需要调用 execute() 方法使其执行.下文中不再特意说明这个问题,大家看代码. 本文中代码样例所使用的 Person 模型如下: class Per ...

  8. python轻量级orm框架 peewee常用功能速查

    peewee常用功能速查 peewee 简介 Peewee是一种简单而小的ORM.它有很少的(但富有表现力的)概念,使它易于学习和直观的使用. 常见orm数据库框架 Django ORM peewee ...

  9. 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 ...

  10. Python:用 peewee 框架连接 SQL Server

    Peewee 默认支持 Sqlite.MySQL.PostgreSQL 三种数据库,如果要使用其他数据库,需要同时安装扩展库.比如 SQL Server,需要安装 peewee-mssql. 但是安装 ...

随机推荐

  1. 大三末java实习生一面凉经

    在南京投了一些小公司,想找个实习,因为知道自己很菜,就收到了一家公司的面试. 面试一般在线上进行,我是在腾讯会议上进行的.面试官其实挺好的,一般不会为难你,因为他知道你是在校生不会懂那么多企业的技术. ...

  2. Vue3 组件之间的数据传递

    1.组件分为:页面级组件和功能组件

  3. 数据对接:从Notion Database到低代码平台

    前言 Notion简介 近几年,有一款叫Notion的产品异常火爆,它是集笔记.任务管理.Wiki.数据管理为一体的产品,他主打两个理念「模块化」和「All-in-one」,Notion最有魅力的还是 ...

  4. 【SDOI2015】星际战争

    #include<cstdio> #include<queue> using namespace std; const int M = 10000; const double ...

  5. JSP 页面引入静态资源 404 未找到

    jsp 页面引入了 css 文件,部署项目时发现 css 不生效,打开 f12 查看网络,发现请求状态码是 404.导致这个问题的情况大概有以下两种情况: 如果你通过浏览器 f12 查看 link 或 ...

  6. OpenLayers入门练习

    一.实验内容 练习OpenLayers的引用形式; 简单地图加载; 控件加载. 二.实验步骤 2.1 ol引用 <!doctype html> <html lang="zh ...

  7. PostgreSQL控制文件讲解及案例

    PostgreSQL控制文件内容: 主要分为是三部分,初始化静态信息.WAL及检查点的动态信息.一些配置信息. 我们可以用过pg_controldata命令直接读取PostgreSQL控制文件内容: ...

  8. VM中centos7创建后无法上网问题

    在VMware 12 Pro上安装完CentOS7发现使用ping www.baidu.com无法ping通 使用以下命令: 1.cd /etc/sysconfig/network-scripts 然 ...

  9. Codeforces Round #748 (Div. 3) - D2. Half of Same

    数论 + 随机化 [Problem - D2 - Codeforces](https://codeforces.com/contest/1749/problem/D) 题意 给定一个长度为 \(n\; ...

  10. 部署mall电商系统踩坑记录

    一. mysql docker run -p 3306:3306 --name mysql -v /mydata/mysql/log:/var/log/mysql -v /mydata/mysql/d ...