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

最新文章

  1. [译]ZOOKEEPER RECIPES-Locks
  2. call,apply学习小结
  3. 2015.10.18 do while练习
  4. 解决在CentOS6.5下安装OpenStack(Icehouse版本 )出现的glance服务无法正常工作的问题
  5. NDK各版本下载
  6. mysql数据库版本引发的问题
  7. spark读取hdfs数据本地性异常
  8. linux源码Makefile详解(完整)【转】
  9. UVA 10651 Pebble Solitaire 状态压缩dp
  10. Dragons
  11. Linux下搭建Oracle11g RAC(9)----创建RAC数据库
  12. MongoDB_1
  13. C语言strtok()函数:字符串分割
  14. C语言 extern3 全局变量的使用
  15. JAVAscript学习笔记 js句柄监听事件 第四节 (原创) 参考js使用表
  16. ModBus功能码速记
  17. 装python package 时,conda提示会升级python2到python3,那可能是你的windows不支持py2env下的此包。
  18. HTTPS的内网访问和访问外网
  19. shiro使用ajax登陆实现,success但页面无法跳转的问题
  20. ios 关于屏幕旋转和屏幕晃动

热门文章

  1. .NET Core MongoDB的简单使用
  2. 运行第一个Go文件
  3. 3 .NET Core笔试题
  4. EPICS Archiver Appliance的定制部署2
  5. 这一次,Python 真的有望告别 GIL 锁了?
  6. Leaflet 调用腾讯瓦片地图服务demo
  7. 推荐系统[八]算法实践总结V2:排序学习框架(特征提取标签获取方式)以及京东推荐算法精排技术实战
  8. webgl 系列 —— 绘制一个点(版本2、版本3、版本4、版本5)
  9. Python ( 高级 第一部)
  10. openssl国密算法库