From 6b3c08100dd7c9e46681b33bfba901b4741b1002 Mon Sep 17 00:00:00 2001 From: dave Date: Sat, 30 Aug 2014 17:55:59 -0400 Subject: [PATCH] Create database if it doesn't exist --- libs/database.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/libs/database.py b/libs/database.py index da4230e..5bdaf53 100644 --- a/libs/database.py +++ b/libs/database.py @@ -1,5 +1,6 @@ #!/usr/bin/env python3 import sqlite3 +import os.path class database: def __init__(self): @@ -10,11 +11,17 @@ class database: def createDatabase(self): queries = [ "CREATE TABLE 'streams' ('id' INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, 'user' INTEGER, 'name' TEXT, 'url' TEXT, 'directory' TEXT, 'status' INTEGER, 'message' TEXT);", - "CREATE TABLE 'times' ('id' INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, 'streamid' INTEGER, 'su' BOOLEAN, 'm' BOOLEAN, 't' BOOLEAN, 'w' BOOLEAN, 'r' BOOLEAN, 'f' BOOLEAN, 'sa' BOOLEAN, 'starthour' INTEGER, 'startmin' INTEGER, 'endhour' INTEGER, 'endmin' INTEGER)", - """INSERT INTO "streams" ("id","user","name","url","directory","status","message") VALUES (NULL,NULL,'WCMF Breakroom','http://1681.live.streamtheworld.com/WCMFFMAAC','wcmf-breakroom','0','')""", - """INSERT INTO "times" ("id","streamid","su","m","t","w","r","f","sa","starthour","startmin","endhour","endmin") VALUES (NULL,'1','0','1','1','1','1','1','0','2','0','7','15')""" + "CREATE TABLE 'times' ('id' INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, 'streamid' INTEGER, 'su' BOOLEAN, 'm' BOOLEAN, 't' BOOLEAN, 'w' BOOLEAN, 'r' BOOLEAN, 'f' BOOLEAN, 'sa' BOOLEAN, 'starthour' INTEGER, 'startmin' INTEGER, 'endhour' INTEGER, 'endmin' INTEGER)", + """INSERT INTO "streams" ("id","user","name","url","directory","status","message") VALUES (NULL,NULL,'WCMF Breakroom','http://1681.live.streamtheworld.com/WCMFFMAAC','wcmf-breakroom','0','')""", + """INSERT INTO "times" ("id","streamid","su","m","t","w","r","f","sa","starthour","startmin","endhour","endmin") VALUES (NULL,'1','0','1','1','1','1','1','0','2','0','7','15')""" ] - + if not os.path.exists("db.sqlite"): + db = sqlite3.connect("db.sqlite", check_same_thread=False, cached_statements=0, isolation_level=None) + cursor = db.cursor() + for query in queries: + cursor.execute(query) + db.close() + def openDB(self): db = sqlite3.connect("db.sqlite", check_same_thread=False, cached_statements=0, isolation_level=None) db.row_factory = self.dict_factory