pip install peewee
安装
当数据库中这些class的数据表不存在的时候,它会自动创建,而且class中不用写字段id,注意要加上这段collation="utf8_general_ci"
不然在存取中文数据的时候会报错~~~~
暂时不清楚什么原理0.0
这里我简单的区分了测试环境–
status = 'test'
def Data():
if status == 'test':
data = {
'host': '127.0.0.1',
'port': '5000',
'user': 'root',
'password': 'root',
'database': 'test'
}
else:
data = {
'host': '127.0.0.1',
'port': '5000',
'user': 'root',
'password': 'root',
'database': 'test'
}
return data
示例:
from peewee import *
from application.config import config
sqlData = config.Data()
db = MySQLDatabase(
host=sqlData['host'],
user=sqlData['user'],
password=sqlData['password'],
database=sqlData['database'],
charset="utf8"
)
class BaseModel(Model):
class Meta:
database = db
class py_rule(BaseModel):
title = CharField(max_length=40, help_text='总规则名称', collation="utf8_general_ci")
user = CharField(max_length=40, help_text='绑定用户', collation="utf8_general_ci")
create_time = CharField(max_length=40, help_text='创建时间', collation="utf8_general_ci")
update_time = CharField(max_length=40, help_text='修改时间', collation="utf8_general_ci")
class py_rule_reply(BaseModel):
rule_id = CharField(max_length=40, help_text='关联总规则的id', collation="utf8_general_ci")
title = CharField(max_length=40, help_text='自规则名称', collation="utf8_general_ci")
user = CharField(max_length=40, help_text='用户id', collation="utf8_general_ci")
touch = CharField(max_length=120, help_text='触发条件', collation="utf8_general_ci")
reply = CharField(max_length=2000, help_text='回复内容', collation="utf8_general_ci")
create_time = CharField(max_length=40, help_text='创建时间', collation="utf8_general_ci")
update_time = CharField(max_length=40, help_text='修改时间', collation="utf8_general_ci")
db.create_tables([py_rule, py_rule_reply])