Skip to content
Snippets Groups Projects
Commit 2468d5a6 authored by Julian Rother's avatar Julian Rother
Browse files

Implement database initialisation

parent bbef90c0
No related branches found
No related tags found
No related merge requests found
*.swp
config.py
__pycache__
*.sqlite
......@@ -9,3 +9,5 @@ DEBUG = True
SQLITE_DB = 'db.sqlite'
DB_ENGINE = 'sqlite'
SQLITE_INIT_SCHEMA = True
SQLITE_INIT_DATA = True
......@@ -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 = {}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment