From 6ed2eea57194f76f5d3f3a5833aa3fa6724505f8 Mon Sep 17 00:00:00 2001 From: James Miller Date: Mon, 11 Nov 2019 16:17:28 -0600 Subject: [PATCH] got actioncost working with yaml generation --- data/yaml/basics.py | 64 +++++++++++++++++++++++++++++++++------------ 1 file changed, 47 insertions(+), 17 deletions(-) diff --git a/data/yaml/basics.py b/data/yaml/basics.py index ad00a53..b832591 100644 --- a/data/yaml/basics.py +++ b/data/yaml/basics.py @@ -1,37 +1,51 @@ import yaml import sqlite3 import os +import pprint DBFILE = 'tmp.db' def main(): # delete DBfile and run fresh os.remove(DBFILE) - # Load in the yaml data with open('basics.yaml') as yl: data = yaml.full_load(yl) - - ## Get database connection - conn = sqlite3.connect(DBFILE) # eventually hook this up to be the main db - ## Set pragmas - pragma1 = "PRAGMA synchronous=OFF;" - pragma2 = "PRAGMA count_changes=OFF;" - pragma3 = "PRAGMA journal_mode=MEMORY;" - pragma4 = "PRAGMA temp_store=MEMORY;" - pragma5 = "PRAGMA foreign_keys=ON;" - conn.execute(pragma1) - conn.execute(pragma2) - conn.execute(pragma3) - conn.execute(pragma4) - conn.execute(pragma5) - + pprint.pprint(data) + # Get a DB conn + conn = get_db_conn() # call the functions to input to SQL do_abilityscore(data['abilityscore'], conn) + do_actioncost(data['actioncost'], conn) + +def do_actioncost(data, conn): + print(data) + + table = """ +CREATE TABLE actioncost ( + actioncost_id INTEGER PRIMARY KEY, + name TEXT NOT NULL UNIQUE, + abbr TEXT NOT NULL UNIQUE +); + """ + + c = conn.cursor() + c.execute(table) + + inp_data = [] + for i in data: + inp_data.append((i['name'], i['abbr'])) + + stmt = "INSERT INTO actioncost(name, abbr) VALUES (?,?)" + try: + conn.executemany(stmt,inp_data) + except: + print("Error creating actioncost") + else: + conn.commit() def do_abilityscore(data, conn): print(data) - print(conn) table = """ CREATE TABLE abilityscore ( @@ -57,6 +71,22 @@ CREATE TABLE abilityscore ( else: conn.commit() +def get_db_conn(): + ## Get database connection + conn = sqlite3.connect(DBFILE) # eventually hook this up to be the main db + ## Set pragmas + pragma1 = "PRAGMA synchronous=OFF;" + pragma2 = "PRAGMA count_changes=OFF;" + pragma3 = "PRAGMA journal_mode=MEMORY;" + pragma4 = "PRAGMA temp_store=MEMORY;" + pragma5 = "PRAGMA foreign_keys=ON;" + conn.execute(pragma1) + conn.execute(pragma2) + conn.execute(pragma3) + conn.execute(pragma4) + conn.execute(pragma5) + return conn + if __name__ == "__main__": main()