diff --git a/.gitignore b/.gitignore index 1a6ee452eb00420e926f9a4887c7ae55b37a8404..d4ec164d8556e9b7247a59c9c186a5cfd65aface 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.swp config.py __pycache__ +*.sqlite diff --git a/config.py b/config.py index d8e980005c1bf70976e5ee8029e1b84f0b663a2a..7c9da570301caae3dc5f6bd58a61ecd54ad27fc4 100644 --- a/config.py +++ b/config.py @@ -9,3 +9,5 @@ DEBUG = True SQLITE_DB = 'db.sqlite' DB_ENGINE = 'sqlite' +SQLITE_INIT_SCHEMA = True +SQLITE_INIT_DATA = True diff --git a/server.py b/server.py index 2442fcf3ae40b91c0e5fee0ae60dc82200a136d6..d1d4aaad609dcc4b0d93c535499f093fa02c76bd 100755 --- a/server.py +++ b/server.py @@ -2,11 +2,27 @@ from flask import Flask, render_template, g import mysql.connector import sqlite3 +import os app = Flask(__name__) config = app.config +config['DB_SCHEMA'] = 'db_schema.sql' +config['DB_DATA'] = 'db_example.sql' +config['SQLITE_INIT_SCHEMA'] = False +config['SQLITE_INIT_DATA'] = False config.from_pyfile('config.py') +if config['DB_ENGINE'] == 'sqlite': + created = not os.path.exists(config['SQLITE_DB']) + db = sqlite3.connect(config['SQLITE_DB']) + cur = db.cursor() + if config['SQLITE_INIT_SCHEMA']: + cur.executescript(open(config['DB_SCHEMA']).read()) + if config['SQLITE_INIT_DATA'] and created: + cur.executescript(open(config['DB_DATA']).read()) + db.commit() + db.close() + # Row wrapper for sqlite def dict_factory(cursor, row): d = {}