34 lines
999 B
Python
34 lines
999 B
Python
|
# -*- coding: utf-8 -*-
|
||
|
from sqlite3 import connect as sqconnect
|
||
|
|
||
|
|
||
|
class DataBase(object):
|
||
|
|
||
|
def __init__(self, path: str, table: str) -> None:
|
||
|
self.path = path
|
||
|
self.table = table
|
||
|
|
||
|
def __execute(self, request: str, additional=None):
|
||
|
with sqconnect(self.path) as db:
|
||
|
cursor = db.cursor()
|
||
|
if additional is not None:
|
||
|
cursor.execute(request, additional)
|
||
|
else:
|
||
|
cursor.execute(request)
|
||
|
db.commit()
|
||
|
|
||
|
def createTable(self, table_content: str) -> None:
|
||
|
self.__execute(f'''
|
||
|
CREATE TABLE IF NOT EXISTS {self.table}(
|
||
|
id INTEGER PRIMARY KEY,
|
||
|
{table_content}
|
||
|
)
|
||
|
''')
|
||
|
|
||
|
def insert(self, data: dict) -> None:
|
||
|
fields = ", ".join(list(data.keys()))
|
||
|
values = list(data.values())
|
||
|
self.__execute(f'''
|
||
|
INSERT INTO {self.table} ({fields}) VALUES ({str("?," * len(data))[:-1]})
|
||
|
''', tuple(values))
|