-
Notifications
You must be signed in to change notification settings - Fork 3
/
database.py
40 lines (36 loc) · 1.21 KB
/
database.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import os
import sqlite3
class DBCore:
db_file = "icons.db"
def __init__(self):
if os.path.exists(self.db_file):
return
with open("schema.sql", 'r') as f:
schema = f.read()
conn = sqlite3.connect(self.db_file)
cursor = conn.cursor()
cursor.executescript(schema)
conn.commit()
cursor.close()
conn.close()
def get_url(self, icon_hash):
"""Returns the icon URL based on the icon sha"""
conn = sqlite3.connect(self.db_file)
cursor = conn.cursor()
cursor.execute("SELECT * FROM tbl_icons WHERE hash=?", (icon_hash,))
result = cursor.fetchone()
cursor.close()
conn.close()
if result is not None:
return result[1]
return None
def set_url(self, icon_hash, url):
"""Sets the URL of an icon hash on the DB"""
conn = sqlite3.connect(self.db_file)
cursor = conn.cursor()
cursor.execute("INSERT INTO tbl_icons(hash, URL) values(?,?)", (icon_hash, url,))
conn.commit()
cursor.close()
conn.close()
if __name__ == '__main__':
db = DBCore()