From 48427ecd21cc4fbd538bc1736815f99434e2f99f Mon Sep 17 00:00:00 2001 From: Mike Bloy Date: Sun, 24 Oct 2010 11:46:49 -0500 Subject: [PATCH] create the database if it doesn't exist, on startup --- dr.botzo.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/dr.botzo.py b/dr.botzo.py index b436cab..f153b3c 100644 --- a/dr.botzo.py +++ b/dr.botzo.py @@ -22,6 +22,7 @@ import re import socket import sys import inspect +import sqlite3 from extlib import irclib @@ -103,6 +104,32 @@ except NoOptionError as e: # load additional options irclib.DEBUG = config.getboolean('dr.botzo', 'debug') +try: + # make sure we can initialize the database, if such a thing is + # called for in the config file + dbfile = config.get('dr.botzo', 'database') + conn = sqlite3.connect(dbfile) + try: + query = """ + SELECT COUNT(*) + FROM sqlite_master + WHERE type = 'table' AND name = 'drbotzo_modules' + """ + row = conn.execute(query).fetchone() + if row[0] == 0: + # need to create the drbotzo_modules table + query = """ + CREATE TABLE drbotzo_modules ( + module TEXT, + version INTEGER + ) + """ + conn.execute(query) + conn.commit() + finally: conn.close() +except NoOptionError: pass # if the config file has no db property, assume that +except NoSectionError: pass # the database doesn't need to exist + # start up the IRC bot # create IRC and server objects and connect