-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLdata.py
More file actions
241 lines (184 loc) · 7.16 KB
/
SQLdata.py
File metadata and controls
241 lines (184 loc) · 7.16 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import sqlite3
DATABASE_NAME = "song_database.db"
#INFORMATION IN DATABASE
'''
(SONG_NAME, SONG_ARTIST, PLAYLIST, SPOT_IN_PLAYLIST, SONGLINK, SONG_DURATION)
'''
connection = sqlite3.connect(DATABASE_NAME)
cursor = connection.cursor()
#Create Initial Table
createTable = '''CREATE TABLE IF NOT EXISTS songs(
song_name TEXT,
song_artist TEXT,
playlist TEXT,
spot_in_playlist INTEGER,
songlink TEXT,
duration INT
);
'''
cursor.execute(createTable)
class Database():
def __init__(self):
pass
def display_table(self):
connection = sqlite3.connect(DATABASE_NAME)
cursor = connection.cursor()
print("Displaying Table")
# Execute a query
cursor.execute("SELECT * FROM songs")
results = cursor.fetchall()
# Display the results
for row in results:
print(row)
connection.commit()
connection.close()
def add_song(self, data:list):
connection = sqlite3.connect(DATABASE_NAME)
cursor = connection.cursor()
name = data[0]
artist = data[1]
playlist = data[2]
spot_in_playlist = data[3]
songlink = data[4]
duration = data[5]
try:
cursor.execute("INSERT INTO songs(song_name, song_artist, playlist, spot_in_playlist, songlink, duration) VALUES(?, ?, ?, ?, ?, ?)",
(name, artist, playlist, spot_in_playlist, songlink, duration))
except sqlite3.Error as e:
print("Error: %s" % e.args[0])
connection.commit()
connection.close()
def find_song(self, column, value):
connection = sqlite3.connect(DATABASE_NAME)
cursor = connection.cursor()
results = []
match column:
case "name":
cursor.execute("SELECT song_name FROM songs")
row = cursor.fetchall()
for i, info in enumerate(row):
if info[0] == value:
cursor.execute("SELECT * FROM songs LIMIT 1 OFFSET ?", (str(i)))
result = cursor.fetchone()
results.append(result)
case "artist":
cursor.execute("SELECT song_artist FROM songs")
row = cursor.fetchall()
for i, info in enumerate(row):
if info[0] == value:
cursor.execute("SELECT * FROM songs LIMIT 1 OFFSET ?", (str(i)))
result = cursor.fetchone()
results.append(result)
case "playlist":
cursor.execute("SELECT playlist FROM songs")
row = cursor.fetchall()
for i, info in enumerate(row):
if info[0] == value:
cursor.execute("SELECT * FROM songs LIMIT 1 OFFSET ?", (str(i)))
result = cursor.fetchone()
results.append(result)
connection.commit()
connection.close()
return results
def add_to_playlist(self, playlist, song_name, song_artist):
connection = sqlite3.connect(DATABASE_NAME)
cursor = connection.cursor()
playlist_spot = self.lowest_available_spot(playlist)
cursor.execute('''UPDATE songs SET playlist = ?, spot_in_playlist = ? WHERE song_name = ? AND song_artist = ?''', (playlist, playlist_spot, song_name, song_artist))
connection.commit()
connection.close()
self.reorder_playlist_spots(playlist)
def lowest_available_spot(self, playlist):
connection = sqlite3.connect(DATABASE_NAME)
cursor = connection.cursor()
try:
cursor.execute("SELECT spot_in_playlist FROM songs WHERE playlist = ?", (playlist,))
results = cursor.fetchall()
used_spots = set(row[0] for row in results if row[0] is not None)
spot = 1
while spot in used_spots:
spot += 1
except sqlite3.Error as e:
print("Error finding available spot:", e)
spot = 1
connection.commit()
connection.close()
return spot
def reorder_playlist_spots(self, playlist):
connection = sqlite3.connect(DATABASE_NAME)
cursor = connection.cursor()
try:
# Get all songs in the playlist, ordered by current spot
cursor.execute("SELECT rowid, * FROM songs WHERE playlist = ? ORDER BY spot_in_playlist ASC", (playlist,))
songs = cursor.fetchall()
# Reassign spots from 1 to len(songs)
for new_spot, song in enumerate(songs, start=1):
row_id = song[0] # Use rowid to uniquely identify the row
cursor.execute("UPDATE songs SET spot_in_playlist = ? WHERE rowid = ?", (new_spot, row_id))
except sqlite3.Error as e:
print("Error reordering playlist spots:", e)
connection.commit()
connection.close()
def delete_song(self, data):
pass
def get_all_songs(self):
connection = sqlite3.connect(DATABASE_NAME)
cursor = connection.cursor()
songs = []
# Execute a query
cursor.execute("SELECT * FROM songs")
results = cursor.fetchall()
for row in results:
songs.append(row)
connection.commit()
connection.close()
return songs
def get_all_artists(self):
connection = sqlite3.connect(DATABASE_NAME)
cursor = connection.cursor()
artists = []
# Execute a query
cursor.execute("SELECT DISTINCT song_artist FROM songs")
results = cursor.fetchall()
for row in results:
artists.append(row)
connection.commit()
connection.close()
return artists
def get_all_playlists(self):
connection = sqlite3.connect(DATABASE_NAME)
cursor = connection.cursor()
# Execute a query
cursor.execute("SELECT playlist FROM songs")
playlists = cursor.fetchall()
true_playlists = []
for playlist in playlists:
if playlist[0] != "None" and playlist[0] not in true_playlists:
true_playlists.append(playlist[0])
connection.commit()
connection.close()
return true_playlists
def get_songs_by_artist(self, artist):
connection = sqlite3.connect(DATABASE_NAME)
cursor = connection.cursor()
songs = []
try:
cursor.execute("SELECT * FROM songs WHERE song_artist = ?", (artist,))
songs = cursor.fetchall()
except sqlite3.Error as e:
print("Error fetching songs from artist:", e)
connection.commit()
connection.close()
return songs
def get_songs_in_playlist(self, playlist):
connection = sqlite3.connect(DATABASE_NAME)
cursor = connection.cursor()
songs = []
try:
cursor.execute("SELECT * FROM songs WHERE playlist = ?", (playlist,))
songs = cursor.fetchall()
except sqlite3.Error as e:
print("Error fetching songs from playlist:", e)
connection.commit()
connection.close()
return songs