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