This repository has been archived by the owner on Dec 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb.py
52 lines (43 loc) · 1.99 KB
/
db.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
41
42
43
44
45
46
47
48
49
50
51
52
# std libs
import sqlite3 as lite
from datetime import datetime
################################################################################
class DB(object):
""" Class that connects to a Database """
def __init__(self, db=None, table_name=None):
super(DB, self).__init__()
db = "insta.db" if db is None else db
self.table_name = "insta_media" if table_name is None else table_name
self._conn = lite.connect(db)
self._cursor = self._conn.cursor()
self.__createTableIfNotExists()
# --------------------------------------------------------------------------
def __createTableIfNotExists(self):
""" If table is does not exist it will create it """
query = "SELECT name FROM sqlite_master WHERE type='table'"
results = self._cursor.execute(query)
name_list = [name for r in results for name in r]
if self.table_name not in name_list:
# Timestamp is for the time from pythons datetime.now() func
table = """CREATE TABLE {}
(media TEXT PRIMARY KEY,
thumbnail TEXT, regular TEXT, large TEXT,
[timestamp] timestamp)
""".format(self.table_name)
self._cursor.execute(table)
# --------------------------------------------------------------------------
def insertValues(self, _id, thumbnail, regular, large):
query = "INSERT INTO {} VALUES ('{}', '{}', '{}', '{}', '{}')".format(
self.table_name, _id, thumbnail, regular, large, datetime.now()
)
self._cursor.execute(query)
self._conn.commit()
# --------------------------------------------------------------------------
def isKeyInDB(self, pk):
query = "SELECT media FROM {}".format(self.table_name)
results = self._cursor.execute(query)
pk_list = [_pk for r in results for _pk in r]
for _pk in pk_list:
if pk == _pk:
return True
return False