Skip to content
Snippets Groups Projects

Store created elements in a local SQLite database

Merged Eva Bardou requested to merge sqlite-database into master
All threads resolved!
2 files
+ 48
33
Compare changes
  • Side-by-side
  • Inline
Files
2
+ 23
19
# -*- coding: utf-8 -*-
import os
import sqlite3
from arkindex_worker import logger
SQL_ELEMENTS_TABLE_CREATION = """CREATE TABLE IF NOT EXISTS elements (
id VARCHAR(32) PRIMARY KEY,
parent_id VARCHAR(32),
name TEXT NOT NULL,
type TEXT NOT NULL,
polygon TEXT,
worker_version_id VARCHAR(32)
)"""
class LocalDB(object):
def __init__(self, path):
if not os.path.exists(path):
open(path, "x").close()
self.db = sqlite3.connect(path)
self.db.row_factory = sqlite3.Row
self.cursor = self.db.cursor()
logger.info(f"Connection to local cache {path} established.")
def create_elements_table(self):
try:
self.cursor.execute(
"""CREATE TABLE elements (
id TEXT PRIMARY KEY,
parent_id TEXT,
name TEXT NOT NULL,
type TEXT NOT NULL,
polygon TEXT,
worker_version_id TEXT
)"""
)
except sqlite3.OperationalError:
print("Table 'elements' already exists")
def create_tables(self):
self.cursor.execute(SQL_ELEMENTS_TABLE_CREATION)
def insert(self, table, lines):
self.cursor.executemany(f"INSERT INTO {table} VALUES (?,?,?,?,?,?)", lines)
if not lines:
return
columns = ", ".join(lines[0].keys())
placeholders = ", ".join("?" * len(lines[0]))
values = [tuple(line.values()) for line in lines]
self.cursor.executemany(
f"INSERT INTO {table} ({columns}) VALUES ({placeholders})", values
)
self.db.commit()
Loading