db db db init
parent
898436252a
commit
ed08dee35c
@ -0,0 +1,37 @@
|
|||||||
|
import sqlite3
|
||||||
|
|
||||||
|
def get_db_connection():
|
||||||
|
conn = sqlite3.connect('database.db')
|
||||||
|
conn.row_factory = sqlite3.Row
|
||||||
|
return conn
|
||||||
|
|
||||||
|
def dict_from_row(row):
|
||||||
|
return dict(zip(row.keys(), row))
|
||||||
|
|
||||||
|
def create_instrument(name, slug, description, params, sockets):
|
||||||
|
connection = get_db_connection()
|
||||||
|
cur = connection.cursor()
|
||||||
|
cur.execute(
|
||||||
|
"INSERT INTO instruments (name, slug, description, panel) VALUES (?, ?, ?, ?)",
|
||||||
|
(name, slug, description, f'{slug}.svg')
|
||||||
|
)
|
||||||
|
|
||||||
|
for param in params:
|
||||||
|
cur.execute(
|
||||||
|
"INSERT OR IGNORE INTO params (param_name) VALUES (?)",(param,)
|
||||||
|
)
|
||||||
|
cur.execute(
|
||||||
|
"INSERT INTO instrument_param (instrument, param_name) VALUES (?, ?)", (slug, param )
|
||||||
|
)
|
||||||
|
|
||||||
|
for socket in sockets:
|
||||||
|
cur.execute(
|
||||||
|
"INSERT OR IGNORE INTO sockets (socket_name) VALUES (?)", (socket,)
|
||||||
|
)
|
||||||
|
|
||||||
|
cur.execute(
|
||||||
|
"INSERT INTO instrument_socket (instrument, socket_name) VALUES (?, ?)", (slug, socket )
|
||||||
|
)
|
||||||
|
|
||||||
|
connection.commit()
|
||||||
|
connection.close()
|
@ -1,16 +1,12 @@
|
|||||||
import sqlite3
|
from db import create_instrument, get_db_connection
|
||||||
|
|
||||||
connection = sqlite3.connect('database.db')
|
connection = get_db_connection()
|
||||||
|
|
||||||
with open('schema.sql') as f:
|
with open('schema.sql') as f:
|
||||||
connection.executescript(f.read())
|
connection.executescript(f.read())
|
||||||
|
|
||||||
cur = connection.cursor()
|
|
||||||
|
|
||||||
cur.execute(
|
create_instrument('Test Instrument', 'test_instrument', 'The classic original the one we like to test', ['eye','back'], ['feet','faat', 'foot', 'nose'] )
|
||||||
"INSERT INTO instruments (name, slug, description, panel) VALUES (?, ?, ?, ?)",
|
|
||||||
('Test Instrument', 'test_instrument', 'The classic original the one we like to test', 'test_instrument.svg')
|
|
||||||
)
|
|
||||||
|
|
||||||
connection.commit()
|
connection.commit()
|
||||||
connection.close()
|
connection.close()
|
Loading…
Reference in New Issue