安装:
pip install peewee
pip install pymysql
使用:
第一步:在项目中创建文件xxx.db
第二步:Navicat打开xxx.db
第三步:建表
第四步:peewee连接数据库:示例 mysql和sqlite
import os
from peewee import *
db_mysql = MySQLDatabase('demo', user='root', password='root', host='localhost', port=3306)
db_sqlite = SqliteDatabase(os.getcwd() + '\demo.db')
class User(Model):
login_name = TextField()
user_name = TextField()
cred_num = TextField()
class Meta:
database = db_mysql
# 显示指定表名
table_name = 'user'
class Account(Model):
id = IntegerField()
username = TextField()
password = TextField()
class Meta:
database = db_sqlite
class Announcement(Model):
# 标题
title = TextField()
# 内容
content = TextField()
# 发布日期
release_date = TextField()
# 浏览次数
read_times = IntegerField()
class Meta:
database = db_mysql
第五步:CRUD
if __name__ == '__main__':
announcement = Announcement(title="标题", content="内容", release_dat="日期", read_times=1)
announcement.save()