def insert_items(db, dict_of_items): insert_statement = '''INSERT INTO items (item_id, category_id, vendor_id, item_description, pack_qty, bottle_volume_ml, state_wholesale, state_retail) VALUES (?, ?, ?, ?, ?, ?, ?, ?)''' with setup.db_connect(db) as database: cur = database.cursor() cur.executemany(insert_statement, dict_of_items.values())
def insert_categories(db, dict_of_categories): insert_statement = '''INSERT INTO categories (category_id, category_name) VALUES(?, ?)''' with setup.db_connect(db) as database: cur = database.cursor() cur.executemany(insert_statement, dict_of_categories.values())
def insert_vendors(db, dict_of_vendors): insert_statement = '''INSERT INTO vendors (vendor_id, vendor_name) VALUES(?, ?)''' with setup.db_connect(db) as database: cur = database.cursor() cur.executemany(insert_statement, dict_of_vendors.values())
def insert_stores(db, dict_of_stores): insert_statment = '''INSERT INTO stores(store_id, store_name, address, city, zip_code, store_lat, store_long, county_id) VALUES(?, ?, ?, ?, ?, ?, ?, ?)''' with setup.db_connect(db) as database: cur = database.cursor() cur.executemany(insert_statment, dict_of_stores.values())
def insert_sales(db, dict_of_sales): insert_statement = '''INSERT INTO sales(sale_id, sale_date, store_id, item_id, bottles_sold, sale_value, sale_vol_ml) VALUES(?, ?, ?, ?, ?, ?, ?)''' with setup.db_connect(db) as database: cur = database.cursor() cur.executemany(insert_statement, dict_of_sales.values())
def create_all_tables(db): print('Creating Tables') with setup.db_connect(db) as database: county_table = setup.CountySchema() database.execute(county_table.create_counties_table()) county_table.insert_counties(database) stores_table = setup.StoreSchema() database.execute(stores_table.create_stores_table()) categories_table = setup.CategorieSchema() database.execute(categories_table.create_categories_table()) vendors_table = setup.VendorSchema() database.execute(vendors_table.create_vendors_table()) items_table = setup.ItemSchema() database.execute(items_table.create_items_table()) sales_table = setup.SaleSchema() database.execute(sales_table.create_sales_table())
print(json.loads(line.decode())) return # End of stream_sample # 異常終了時の処理 def signal_handler(sig, stack): if db_connection is not None: db_connection.close() print('signal_handler(): %d, %s' % (sig, str(stack))) raise SystemExit('Exiting') return signal.signal(signal.SIGINT, signal_handler) signal.signal(signal.SIGUSR1, signal_handler) if __name__ == '__main__': try: db_connect() stream_all() except: print("Error is occured..") quit() finally: db_connection.close()
from db_setup import db_connect from db_setup import db_insert_to_dict def register_dict(): try: f = open('config.yml', 'r') data = yaml.load(f) f.close() dictionaries=data['DICT'] db_insert_to_dict(dictionaries) except IOError as e : print("Config file \"{0}\" is not found.".format(e)) raise return #-End-of-register_dict() if __name__ == '__main__': try: db_connect() register_dict() except: print("Error is occured..") quit() finally: if db_connection is not None: db_connection.close()