2021-01-02 15:32:04 +01:00
|
|
|
import sqlite3
|
|
|
|
from sqlite3 import Error
|
|
|
|
|
|
|
|
|
2021-05-30 14:09:27 +02:00
|
|
|
class sqlite:
|
2021-01-02 15:32:04 +01:00
|
|
|
def __init__(self, db_file):
|
|
|
|
self.conn = self.connect(db_file)
|
|
|
|
|
|
|
|
def connect(self, db_file):
|
|
|
|
conn = None
|
|
|
|
try:
|
|
|
|
conn = sqlite3.connect(db_file)
|
|
|
|
return conn
|
|
|
|
except Error as e:
|
|
|
|
print(e)
|
|
|
|
return conn
|
|
|
|
|
|
|
|
def execute(self, query, params=()):
|
|
|
|
try:
|
|
|
|
self.conn.row_factory = sqlite3.Row
|
|
|
|
c = self.conn.cursor()
|
|
|
|
c.execute(query, params)
|
|
|
|
return [dict(row) for row in c.fetchall()]
|
|
|
|
except Error as e:
|
|
|
|
print("sqlite_execute failed for: {}, {}".format(query, params))
|
|
|
|
print("Error:", e)
|
|
|
|
|
|
|
|
def executemany(self, query, data):
|
|
|
|
try:
|
|
|
|
c = self.conn.cursor()
|
|
|
|
c.executemany(query, data)
|
|
|
|
except Error as e:
|
|
|
|
print("sqlite_executemany failed for: {}, {}".format(query, data))
|
|
|
|
print("Error:", e)
|
|
|
|
|
|
|
|
def create_table(self, t):
|
2021-05-30 14:09:27 +02:00
|
|
|
if t.startswith("datalabel"):
|
2021-01-02 15:32:04 +01:00
|
|
|
query = """CREATE TABLE {} (
|
|
|
|
id real,
|
|
|
|
name text,
|
|
|
|
title text,
|
|
|
|
tooltip text,
|
|
|
|
unit text
|
2021-05-30 14:09:27 +02:00
|
|
|
);""".format(
|
|
|
|
t
|
|
|
|
)
|
|
|
|
elif t.startswith("parameterlijst"):
|
2021-01-02 15:32:04 +01:00
|
|
|
query = """
|
|
|
|
CREATE TABLE {} (
|
|
|
|
id real,
|
|
|
|
name text,
|
|
|
|
name_factory text,
|
|
|
|
min real,
|
|
|
|
max real,
|
|
|
|
def real,
|
|
|
|
title text,
|
|
|
|
description text,
|
|
|
|
unit text
|
2021-05-30 14:09:27 +02:00
|
|
|
);""".format(
|
|
|
|
t
|
|
|
|
)
|
|
|
|
elif t.startswith("versiebeheer"):
|
2021-01-02 15:32:04 +01:00
|
|
|
query = """
|
|
|
|
CREATE TABLE {} (
|
|
|
|
version integer primary key,
|
|
|
|
datalabel integer,
|
|
|
|
parameterlist integer
|
2021-05-30 14:09:27 +02:00
|
|
|
);""".format(
|
|
|
|
t
|
|
|
|
)
|
2021-01-02 15:32:04 +01:00
|
|
|
self.execute(query)
|
|
|
|
self.conn.commit()
|
|
|
|
|
|
|
|
def insert(self, t, data):
|
2021-05-30 14:09:27 +02:00
|
|
|
if t.startswith("datalabel"):
|
2021-01-02 15:32:04 +01:00
|
|
|
query = """
|
|
|
|
INSERT INTO {} (id, name, title, tooltip, unit)
|
|
|
|
VALUES (?, ?, ?, ?, ?);
|
2021-05-30 14:09:27 +02:00
|
|
|
""".format(
|
|
|
|
t
|
|
|
|
)
|
|
|
|
elif t.startswith("parameterlijst"):
|
2021-01-02 15:32:04 +01:00
|
|
|
query = """
|
|
|
|
INSERT INTO {} (id, name, name_factory, min, max, def, title, description, unit)
|
|
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);
|
2021-05-30 14:09:27 +02:00
|
|
|
""".format(
|
|
|
|
t
|
|
|
|
)
|
|
|
|
elif t.startswith("versiebeheer"):
|
2021-01-02 15:32:04 +01:00
|
|
|
query = """
|
|
|
|
INSERT INTO {} (version, datalabel, parameterlist)
|
|
|
|
VALUES (?, ?, ?);
|
2021-05-30 14:09:27 +02:00
|
|
|
""".format(
|
|
|
|
t
|
|
|
|
)
|
2021-01-02 15:32:04 +01:00
|
|
|
self.executemany(query, data)
|
|
|
|
self.conn.commit()
|