Skip to content

Instantly share code, notes, and snippets.

@opalczynski
Created October 5, 2021 12:49
Show Gist options
  • Select an option

  • Save opalczynski/604d5243cdcf3f9838cd517bab1c0c17 to your computer and use it in GitHub Desktop.

Select an option

Save opalczynski/604d5243cdcf3f9838cd517bab1c0c17 to your computer and use it in GitHub Desktop.
SQLite database for the tasks management
import os.path
import sqlite3
class Database:
PROJECT_SQL = """CREATE TABLE project (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name text,
description text NULL);"""
TASK_SQL = """CREATE TABLE task (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title text,
description text,
status integer,
project_id integer,
FOREIGN KEY(project_id) REFERENCES project(id));"""
def _db_exists(self):
return os.path.exists("todos.db")
def get_connection(self):
return sqlite3.connect("todos.db")
def initialize(self):
if self._db_exists():
return
connection = self.get_connection()
cursor = connection.cursor()
cursor.execute(self.PROJECT_SQL)
cursor.execute(self.TASK_SQL)
connection.commit()
connection.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment