8.pythonORM框架还是peewee好用——python学习笔记

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])

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值