mirror of
https://gist.github.com/6ba37e4d4084e858f917e271550ce5f6.git
synced 2024-09-20 00:34:20 +03:00
38 lines
1.1 KiB
Python
38 lines
1.1 KiB
Python
from datetime import datetime
|
|
import sqlite3
|
|
import logging
|
|
|
|
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()
|
|
|
|
def is_exists(self, id) -> bool:
|
|
conn = sqlite3.connect(self.db_name)
|
|
c = conn.cursor()
|
|
c.execute("SELECT EXISTS(SELECT 1 FROM danbooru WHERE id=?)", (id, ))
|
|
result = c.fetchone()[0]
|
|
conn.close()
|
|
return bool(result)
|
|
|
|
def add(self, id, tags):
|
|
conn = sqlite3.connect(self.db_name)
|
|
c = conn.cursor()
|
|
sql = 'INSERT INTO danbooru(id, tags, created_at) VALUES (?,?,?)'
|
|
c.execute(sql, (id, tags, datetime.now()))
|
|
conn.commit()
|
|
conn.close()
|
|
|