import logging import sqlite3 import config class connect: conn = None def __enter__(self): self.conn = sqlite3.connect(config.DB_PATH) self.cursor = self.conn.cursor() return self def __exit__(self, type, value, traceback): if traceback: logging.error(traceback) self.conn.close() def execute(self, stmt, *params): self.cursor.execute(stmt, params) self.conn.commit() def select(self, stmt, *params): res = [] for row in self.cursor.execute(stmt, params): if len(row) == 1: res.append(row[0]) else: res.append(row) return res def init(): logging.info('Initializing database at %s...', config.DB_PATH) with connect() as conn: # conn.execute('drop table if exists localities;') conn.execute(''' create table if not exists localities ( id integer not null primary key, name varchar(255) not null ); ''') # conn.execute('drop table if exists locality_relationships;') conn.execute(''' create table if not exists locality_relationships ( id varchar(255) not null primary key, parent integer not null, child integer not null ) ''') conn.execute(''' create table if not exists search_results ( id integer not null primary key, time datetime not null, property_id integer not null, raw_json text not null ) ''') conn.execute(''' create table if not exists properties ( id integer not null primary key, last_update datetime not null, raw_json text not null ) ''') init()