-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdatabase.py
More file actions
executable file
·185 lines (130 loc) · 4.83 KB
/
database.py
File metadata and controls
executable file
·185 lines (130 loc) · 4.83 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import sqlite3
import os
class MusicDatabase:
def __init__(self):
self.db = None
def doesDatabaseExist(self):
# OBJECTIVE: Check if database exists. This function will stop music.addMusicToDatabase() from adding same songs on bootup.
# Check if file exists
return os.path.exists("Music.db")
def createDatabase(self):
# OBJECTIVE: Create a database if one doesn't exist
# Create a new database and table
self.db = sqlite3.connect("Music.db")
self.db.execute("""CREATE TABLE music
(Artist TEXT NOT NULL,
Album TEXT NOT NULL,
Song TEXT NOT NULL,
Location TEXT NOT NULL UNIQUE,
Pos INTEGER)"""
)
def connectToDatabase(self):
# OBJECTIVE: Check whether or not database exists. If not, call another function to create one.
# Connect to database
if self.doesDatabaseExist():
self.db = sqlite3.connect("Music.db")
# Create a database
else:
self.createDatabase()
def closeConnection(self):
# NOTE: All connections to database must be closed
self.db.close()
def insert(self, artist, album, song, location):
# OBJECTIVE: Insert new data to Music.db
"""
NOTE:
1. INSERT INTO music => Enter data into music table
2. VALUES (?, ?, ?, ?) => Insert following values. Question marks were added to prevent SQL injection attack
"""
# Connect to database
self.connectToDatabase()
try:
# Insert new information and commit it (save changes)
self.db.execute("""INSERT INTO music VALUES (?, ?, ?, ?, ?)""", (artist, album, song, location, 0))
self.db.commit()
except sqlite3.IntegrityError as err:
print("Duplicate entry! Error: {}".format(err))
# Disconnect from database
self.db.close()
def reorganizeDatabase(self):
# OBJECTIVE: Sort database by Song and create a new column labeled index
# Connect to database
self.connectToDatabase()
# Create a cursor and execute sqlite command
rows = self.db.execute("""SELECT * FROM music ORDER BY Song""")
# Update index columns
for indx, row in enumerate(rows):
self.db.execute("""UPDATE music
SET Pos=(?)
WHERE Location=(?)""", (indx, row[3]))
# Save changes
self.db.commit()
# Disconnect from database
self.db.close()
def getSongByName(self, name):
# OBJECTIVE: Based on song's name, return song's pathname.
# Connect to database
self.connectToDatabase()
# Create a cursor and execute sqlite command
row = self.db.execute("""SELECT Song, Location, Pos
FROM music
WHERE Song=(?)""", (name,)) # <= Need to make it into a tuple and add a comma
# If data wasn't found, return
if row == None:
print("Song doesn't exist!")
else:
# Save data from row before disconnection from server
# NOTE: fetchone => execute() returns all rows meeting the criteria. fetchone() gets the 1st row
# Also, fetchone() returns a tuple (name, pathname)
row = row.fetchone()
# Disconnect from database and return output
self.db.close()
return row
def getSongByPos(self, pos):
# OBJECTIVE: Return a nearby song if a user selects Forward or Rewind
# Connect to database
self.connectToDatabase()
# Create a cursor and execute sqlite command
# If Pos is out of bounds, then it's non-existent so return the 1st song as default value
row = self.db.execute("""SELECT Song, Location, Pos
FROM music
WHERE Pos=(?)""", (pos,)) # <= Need to make it into a tuple and add a comma
# Save data from row before disconnection from server
# NOTE: fetchone => execute() returns all rows meeting the criteria. fetchone() gets the 1st row
# Also, fetchone() returns a tuple (name, pathname)
row = row.fetchone()
# If data wasn't found, return
if row == None:
print("Song doesn't exist!")
# Create a cursor and get 1st row as default value
row = self.db.execute("""SELECT Song, Location, Pos
FROM music
WHERE Pos=0""")
row = row.fetchone()
# Disconnect from database and return output
self.db.close()
return row
def printTable(self):
# OBJECTIVE: Print table with cursor
# Connect to database
self.connectToDatabase()
# Create a cursor
cursor = self.db.execute("""SELECT * FROM music
ORDER BY Song""")
# Print table
for pos, row in enumerate(cursor):
print(row)
# Disconnect from database
self.db.close()
def printSongsAvailable(self):
# OBJECTIVE: Print all songs inserted to database
# Connect to database
self.connectToDatabase()
# Create a crusor
cursor = self.db.execute("""SELECT Song FROM music
ORDER BY Song""")
# Only print "song" column
for pos, row in enumerate(cursor):
print("{}. {}".format(pos + 1, row[0]))
# Disconnect from database
self.db.close()