Exemplo n.º 1
0
class Database(object):
    def __init__(self, database=None):

        self.database = database

        if self.database is None:
            self.load_database()

        self.Model = self.get_model_class()

    def load_database(self):
        self.database = SqliteDatabase(settings.SQLITE_FILEPATH)

    def get_model_class(self):
        class BaseModel(Model):
            class Meta:
                database = self.database

        return BaseModel

    def connect_db(self):
        self.database.connect()

    def close_db(self):
        if not self.database.is_closed():
            self.database.close()
Exemplo n.º 2
0
class KarmaDB(object):
    def __init__(self, app):
        self.app = app
        self.database = SqliteDatabase(app.config['DATABASE'])
        self.register_handlers()
        self.Model = self.get_model_class()

    def connect(self):
        if self.database.is_closed():
            self.database.connect()

    def close(self, exc):
        if not self.database.is_closed():
            self.database.close()

    def register_handlers(self):
        self.app.before_request(self.connect)
        self.app.teardown_request(self.close)

    def get_model_class(self):
        class BaseModel(Model):
            class Meta:
                database = self.database
        return BaseModel
Exemplo n.º 3
0
from peewee import OperationalError

__all__ = [
    'db',
    'User',
    'User_subscribe_list',
]

_current_path = os.path.dirname(os.path.realpath(__file__))

_path = '{}/.db'.format(_current_path)

# 本地 执行sqlite写入
_connect = SqliteDatabase(_path)

_connect.is_closed() and _connect.connect()


class _Base(Model):
    # #将表和数据库连接
    class Meta:
        database = _connect


class User(_Base):
    """用户数据表
  id chat_id create_time
  """
    chat_id = IntegerField(index=True, unique=True)
    create_time = DateTimeField('%Y-%m-%d %H:%M:%S', index=True)