39 lines
900 B
Python
39 lines
900 B
Python
from config import conf
|
|
from peewee import MySQLDatabase
|
|
from peewee import SqliteDatabase
|
|
from peewee import Model
|
|
from peewee import AutoField, DateField, IntegerField
|
|
|
|
from peewee import IntegrityError
|
|
|
|
backend = conf["db"].get("backend", None)
|
|
if backend == "sqlite":
|
|
# SQLite database
|
|
db = SqliteDatabase(conf["sqlite"].get("filename", "sqlite.db"))
|
|
elif backend == "mysql":
|
|
db = MySQLDatabase(
|
|
conf["mysql"].get("database"),
|
|
user=conf["mysql"].get("user"),
|
|
password=conf["mysql"].get("password"),
|
|
host=conf["mysql"].get("host", "localhost"),
|
|
port=conf["mysql"].getint("port", 3306),
|
|
)
|
|
else:
|
|
db = None
|
|
|
|
|
|
class BaseModel(Model):
|
|
class Meta:
|
|
database = db
|
|
|
|
|
|
class Price(BaseModel):
|
|
id = AutoField()
|
|
date = DateField(unique=True)
|
|
price = IntegerField()
|
|
|
|
|
|
def init():
|
|
db.connect()
|
|
db.create_tables([Price])
|