This repository was archived by the owner on Aug 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_utilities.py
103 lines (90 loc) · 3.05 KB
/
db_utilities.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
102
103
import sqlite3
# Initial setup for DB
# WARNING: Only use this to create a new DB. It may replace a db of
# the same name if it already exists.
def setup_db():
db = sqlite3.connect('data.db')
cursor = db.cursor()
cursor.execute('''CREATE TABLE repos(id INTEGER PRIMARY KEY, name TEXT,
user TEXT, language TEXT, stars INTEGER,
contributors INTEGER, forks INTEGER)''')
db.commit()
# Add data to DB
def add_data(name, user, language, stars, contributors, forks):
try:
db = sqlite3.connect('data.db')
cursor = db.cursor()
cursor.execute('''INSERT INTO repos(name, user, language, stars, contributors, forks)
VALUES(?,?,?,?,?,?)''', (name, user, language, stars, contributors, forks))
db.commit()
except Exception as Error:
print(Error)
db.rollback()
raise Error
finally:
db.close()
# Read data from the db at a specific user ID
def read_data(user_id):
try:
db = sqlite3.connect('data.db')
cursor = db.cursor()
cursor.execute('''SELECT name, user, language, stars, contributors, forks FROM repos WHERE id=?''', (user_id,))
repo = cursor.fetchone() #retrieve the user at user ID
# Print out all the data at that user_id
print("Name: " + repo[0])
print("User: " + repo[1])
print("language: " + repo[2])
print("Stars: " + str(repo[3]))
print("Contrib: " + str(repo[4]))
print("Forks: " + str(repo[5]))
print("ID: " + str(user_id))
print("")
except Exception as Error:
print(Error)
db.rollback()
raise Error
finally:
db.close()
# Read data from the db at the last index
def read_last_insert():
try:
db = sqlite3.connect('data.db')
cursor = db.cursor()
cursor.execute('''SELECT * FROM repos WHERE ID = (SELECT MAX(ID) FROM repos);''');
repo = cursor.fetchone() #retrieve the user at user ID
# Print out all the data at that user_id
print("ID: " + str(repo[0]))
print("Name: " + repo[1])
print("User: " + repo[2])
try:
print("language: " + repo[3])
except:
pass
print("Stars: " + str(repo[4]))
print("Contrib: " + str(repo[5]))
print("Forks: " + str(repo[6]))
print("")
except Exception as Error:
print(Error)
db.rollback()
raise Error
finally:
db.close()
# Checks if the entry already exists
def check_db(name, user):
db = sqlite3.connect('data.db')
cursor = db.cursor()
cursor.execute('''SELECT name, user FROM repos''')
all_rows = cursor.fetchall()
for row in all_rows:
if (row[0] == name) and (row[1] == user):
db.close()
return 1
db.close()
return 0
# If run from main you can create a new database and table if
# you want to start fresh...
if __name__ == "__main__":
userin = input("Do you really want to create a new database?")
if userin:
setup_db()