def db_init(): DB.connect() try: DB.create_tables([User]) print('Creating tables...') except OperationalError: pass DB.close() redis.delete('chatcount')
def check_and_create_device(self, info, addr) -> Optional[Device]: device_info = super().check_and_create_device(info, addr) if device_info is not None: dev = DBDevice.new_from_device_info(info.name, device_info) dev.enabled = True DB.connect() db_dev = DBDevice.find(DB.session, dev.identifier) if db_dev is None: DB.session.add(dev) elif db_dev.address != dev.address: db_dev.address = dev.address DB.close() return device_info
from config import DB as db from models import * db.connect() db.create_tables([User, Message]) db.close()
from peewee import * from config import DB class Person(Model): firstname = CharField() surname = CharField() class Meta: database = DB if __name__ == "__main__": DB.connect() DB.create_tables([Person]) # Add some data Person(firstname="John", surname="Bob").save() Person(firstname="Jane", surname="Bob").save() Person(firstname="Michael", surname="Clark").save()
def _db_connect(): if not type(DB) == SqliteDatabase: DB.connect()
def connect(request): try: DB.connect() except: print("unexpected error in connecting to database")