1
0
mirror of https://github.com/pommi/python-itho-wpu.git synced 2024-11-21 13:52:15 +01:00
python-itho-wpu/db.py
Pim van den Berg 8b34069fc6 feat(convert-itho-db): convert an itho servicetool database to sqlite
The SQLite database (a subset of the Itho database) can be used to
dynamically interact with an Itho WPU based on the WPU version.

> usage: convert-itho-db.py [-h] --itho-db [ITHO_DB] [--sqlite-db [SQLITE_DB]]
>                           [--force]
>
> Convert Itho Servicetool database to SQLite
>
> optional arguments:
>   -h, --help            show this help message and exit
>   --itho-db [ITHO_DB]   Itho Database file (default: None)
>   --sqlite-db [SQLITE_DB]
>                         Itho Database file (default: heatpump.sqlite)
>   --force               Force overwrite SQLite database (default: False)
2021-01-02 15:53:47 +01:00

86 lines
2.6 KiB
Python

import sqlite3
from sqlite3 import Error
class sqlite():
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):
if t.startswith('datalabel'):
query = """CREATE TABLE {} (
id real,
name text,
title text,
tooltip text,
unit text
);""".format(t)
elif t.startswith('parameterlijst'):
query = """
CREATE TABLE {} (
id real,
name text,
name_factory text,
min real,
max real,
def real,
title text,
description text,
unit text
);""".format(t)
elif t.startswith('versiebeheer'):
query = """
CREATE TABLE {} (
version integer primary key,
datalabel integer,
parameterlist integer
);""".format(t)
self.execute(query)
self.conn.commit()
def insert(self, t, data):
if t.startswith('datalabel'):
query = """
INSERT INTO {} (id, name, title, tooltip, unit)
VALUES (?, ?, ?, ?, ?);
""".format(t)
elif t.startswith('parameterlijst'):
query = """
INSERT INTO {} (id, name, name_factory, min, max, def, title, description, unit)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);
""".format(t)
elif t.startswith('versiebeheer'):
query = """
INSERT INTO {} (version, datalabel, parameterlist)
VALUES (?, ?, ?);
""".format(t)
self.executemany(query, data)
self.conn.commit()