forked from bitdagger/mtg-scanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
storagedb.py
101 lines (86 loc) · 2.91 KB
/
storagedb.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
from __future__ import print_function
import os
import sqlite3
import sys
import urllib
import json
import phash
import time
from progressbar import Bar, Counter, ETA, Percentage, ProgressBar
from datetime import datetime as dt
"""Storage database module
This module handles storing the user's card collection and retrieving stored
cards.
"""
class MTG_Storage_DB(object):
"""Attributes:
PATH (string): Path of the storage database file
connection (sqlite): Established sqlite connection
"""
PATH = 'storage.db'
def __init__(self, database):
self.PATH = "cards/%s.db" % (database,)
try:
self.connection = sqlite3.connect(self.PATH)
except sqlite3.Error, e:
print("Error %s:" % e.args[0])
sys.exit(1)
def check_rebuild(self):
"""Check to see if the database needs to be rebuilt
"""
rebuild = False
try:
cursor = self.connection.cursor()
cursor.execute("""SELECT name FROM sqlite_master
WHERE type='table';""")
data = cursor.fetchall()
if (not len(data)):
rebuild = True
else:
if ((u'Cards',) not in data):
rebuild = True
except sqlite3.Error, e:
print("Error %s:" % e.args[0])
sys.exit(1)
return rebuild
def do_rebuild(self):
"""Rebuild the database
"""
try:
cursor = self.connection.cursor()
cursor.execute("DROP TABLE IF EXISTS Cards")
cursor.execute("""CREATE TABLE Cards (ID INTEGER NOT NULL
PRIMARY KEY, MultiverseID INTEGER NOT NULL,
Foil INTEGER NOT NULL)""")
self.connection.commit()
except sqlite3.Error, e:
self.connection.rollback()
print("Error %s:" % e.args[0])
sys.exit(1)
def add_card(self, MultiverseID, foil):
"""Add a new card to the database
"""
try:
cursor = self.connection.cursor()
cursor.execute("""INSERT INTO Cards (MultiverseID, Foil)
VALUES(?, ?)""", (MultiverseID, foil))
self.connection.commit()
except sqlite3.Error, e:
self.connection.rollback()
print("Error %s:" % e.args[0])
sys.exit(1)
def get_all(self):
"""Get all the cards that have been entered in the database
"""
try:
cursor = self.connection.cursor()
cursor.execute("""SELECT MultiverseID, Foil, COUNT(*) FROM Cards
GROUP BY MultiverseID, Foil""")
r = cursor.fetchall()
if (r is None):
return []
else:
return r
except sqlite3.Error, e:
print("Error %s:" % e.args[0])
sys.exit(1)