2021-04-14 23:23:56 +03:00
|
|
|
import sqlite3
|
2021-04-15 12:30:26 +03:00
|
|
|
from datetime import datetime
|
|
|
|
|
2021-04-14 23:23:56 +03:00
|
|
|
|
|
|
|
class Database:
|
|
|
|
def __init__(self):
|
|
|
|
self.db_name = 'images.db'
|
|
|
|
self.__create_tables()
|
|
|
|
|
|
|
|
def __create_tables(self):
|
|
|
|
conn = sqlite3.connect(self.db_name)
|
|
|
|
c = conn.cursor()
|
|
|
|
c.executescript("""
|
|
|
|
CREATE TABLE IF NOT EXISTS danbooru (
|
|
|
|
id INTEGER PRIMARY KEY NOT NULL UNIQUE,
|
|
|
|
tags TEXT NOT NULL,
|
|
|
|
created_at TIMESTAMP
|
|
|
|
);
|
|
|
|
""")
|
|
|
|
conn.commit()
|
|
|
|
conn.close()
|
|
|
|
|
2021-04-15 12:30:26 +03:00
|
|
|
def is_exists(self, _id) -> bool:
|
2021-04-14 23:23:56 +03:00
|
|
|
conn = sqlite3.connect(self.db_name)
|
|
|
|
c = conn.cursor()
|
2021-04-15 12:30:26 +03:00
|
|
|
c.execute("SELECT EXISTS(SELECT 1 FROM danbooru WHERE id=?)", (_id, ))
|
2021-04-14 23:23:56 +03:00
|
|
|
result = c.fetchone()[0]
|
|
|
|
conn.close()
|
|
|
|
return bool(result)
|
|
|
|
|
2021-04-15 12:30:26 +03:00
|
|
|
def add(self, _id, tags):
|
2021-04-14 23:23:56 +03:00
|
|
|
conn = sqlite3.connect(self.db_name)
|
|
|
|
c = conn.cursor()
|
|
|
|
sql = 'INSERT INTO danbooru(id, tags, created_at) VALUES (?,?,?)'
|
2021-04-15 12:30:26 +03:00
|
|
|
c.execute(sql, (_id, tags, datetime.now()))
|
2021-04-14 23:23:56 +03:00
|
|
|
conn.commit()
|
|
|
|
conn.close()
|