from peewee import *
db = MySQLDatabase('my_database', user='xxx', password='P@x',
host='xxxxxx', port=3306)
class User(Model):
name = CharField()
email = CharField()
class Meta:
database = db
class Score(Model):
num = IntegerField()
owner = ForeignKeyField(User, backref='score')
class Meta:
database = db
db.create_tables([User])
db.create_tables([Score])
######################插入数据######################
# 方式一
user1 = User.create(name="吴森", email="[email protected]")
# 方式二
user2 = User(name="萧炎", email="[email protected]")
user2.save()
# 方式三 批量创建
users = [
User(name="批量1", email="[email protected]"),
User(name="批量2", email="[email protected]"),
]
User.bulk_create(users)
标签:name,22,Python,peewee,db,class,User,com,email
From: https://blog.51cto.com/wusen/6223703