got actioncost working with yaml generation
parent
998f331bcc
commit
6ed2eea571
|
@ -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()
|
||||
|
|
Loading…
Reference in New Issue