refactor sql database for song, playlist, and song_playlist
This commit is contained in:
parent
3ec08d8446
commit
863c3d417b
@ -113,7 +113,7 @@ class MusicTable(QTableView):
|
|||||||
selected_indices = self.get_selected_rows()
|
selected_indices = self.get_selected_rows()
|
||||||
for file in selected_filepaths:
|
for file in selected_filepaths:
|
||||||
with DBA.DBAccess() as db:
|
with DBA.DBAccess() as db:
|
||||||
db.execute("DELETE FROM library WHERE filepath = ?", (file,))
|
db.execute("DELETE FROM song WHERE filepath = ?", (file,))
|
||||||
for index in selected_indices:
|
for index in selected_indices:
|
||||||
self.model.removeRow(index)
|
self.model.removeRow(index)
|
||||||
|
|
||||||
@ -264,7 +264,7 @@ class MusicTable(QTableView):
|
|||||||
# Update the db
|
# Update the db
|
||||||
with DBA.DBAccess() as db:
|
with DBA.DBAccess() as db:
|
||||||
db.query(
|
db.query(
|
||||||
"UPDATE library SET filepath = ? WHERE filepath = ?",
|
"UPDATE song SET filepath = ? WHERE filepath = ?",
|
||||||
(new_path, filepath),
|
(new_path, filepath),
|
||||||
)
|
)
|
||||||
print(f"Moved: {filepath} -> {new_path}")
|
print(f"Moved: {filepath} -> {new_path}")
|
||||||
@ -296,7 +296,7 @@ class MusicTable(QTableView):
|
|||||||
try:
|
try:
|
||||||
with DBA.DBAccess() as db:
|
with DBA.DBAccess() as db:
|
||||||
data = db.query(
|
data = db.query(
|
||||||
"SELECT id, title, artist, album, genre, codec, album_date, filepath FROM library;",
|
"SELECT id, title, artist, album, genre, codec, album_date, filepath FROM song;",
|
||||||
(),
|
(),
|
||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|||||||
2
main.py
2
main.py
@ -384,7 +384,7 @@ if __name__ == "__main__":
|
|||||||
os.makedirs(path_as_string)
|
os.makedirs(path_as_string)
|
||||||
# Create database on first run
|
# Create database on first run
|
||||||
with DBA.DBAccess() as db:
|
with DBA.DBAccess() as db:
|
||||||
with open("utils/delete_and_create_library.sql", "r") as file:
|
with open("utils/init.sql", "r") as file:
|
||||||
lines = file.read()
|
lines = file.read()
|
||||||
for statement in lines.split(";"):
|
for statement in lines.split(";"):
|
||||||
print(f"executing [{statement}]")
|
print(f"executing [{statement}]")
|
||||||
|
|||||||
@ -64,7 +64,7 @@ def add_files_to_library(files):
|
|||||||
if len(insert_data) >= 1000:
|
if len(insert_data) >= 1000:
|
||||||
with DBA.DBAccess() as db:
|
with DBA.DBAccess() as db:
|
||||||
db.executemany(
|
db.executemany(
|
||||||
"INSERT OR IGNORE INTO library (filepath, title, album, artist, genre, codec, album_date, bitrate) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
|
"INSERT OR IGNORE INTO song (filepath, title, album, artist, genre, codec, album_date, bitrate) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
|
||||||
insert_data,
|
insert_data,
|
||||||
)
|
)
|
||||||
insert_data = [] # Reset the insert_data list
|
insert_data = [] # Reset the insert_data list
|
||||||
@ -72,7 +72,7 @@ def add_files_to_library(files):
|
|||||||
if insert_data:
|
if insert_data:
|
||||||
with DBA.DBAccess() as db:
|
with DBA.DBAccess() as db:
|
||||||
db.executemany(
|
db.executemany(
|
||||||
"INSERT OR IGNORE INTO library (filepath, title, album, artist, genre, codec, album_date, bitrate) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
|
"INSERT OR IGNORE INTO song (filepath, title, album, artist, genre, codec, album_date, bitrate) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
|
||||||
insert_data,
|
insert_data,
|
||||||
)
|
)
|
||||||
return True
|
return True
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
DROP TABLE IF EXISTS library;
|
DROP TABLE IF EXISTS song;
|
||||||
|
|
||||||
CREATE TABLE library(
|
CREATE TABLE song(
|
||||||
-- In SQLite, a column with type INTEGER PRIMARY KEY is an alias for the ROWID (except in WITHOUT ROWID tables) which is always a 64-bit signed integer.
|
-- In SQLite, a column with type INTEGER PRIMARY KEY is an alias for the ROWID (except in WITHOUT ROWID tables) which is always a 64-bit signed integer.
|
||||||
id integer primary key,
|
id integer primary key,
|
||||||
filepath varchar(511) UNIQUE,
|
filepath varchar(511) UNIQUE,
|
||||||
|
|||||||
32
utils/init.sql
Normal file
32
utils/init.sql
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
DROP TABLE IF EXISTS song_playlist;
|
||||||
|
DROP TABLE IF EXISTS song;
|
||||||
|
DROP TABLE IF EXISTS playlist;
|
||||||
|
|
||||||
|
CREATE TABLE song(
|
||||||
|
-- In SQLite, a column with type INTEGER PRIMARY KEY is an alias for the ROWID (except in WITHOUT ROWID tables) which is always a 64-bit signed integer.
|
||||||
|
id integer primary key,
|
||||||
|
filepath varchar(511) UNIQUE,
|
||||||
|
title varchar(255),
|
||||||
|
album varchar(255),
|
||||||
|
artist varchar(255),
|
||||||
|
genre varchar(255),
|
||||||
|
codec varchar(15),
|
||||||
|
album_date date,
|
||||||
|
bitrate int,
|
||||||
|
date_added TIMESTAMP default CURRENT_TIMESTAMP
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE playlist(
|
||||||
|
id integer primary key,
|
||||||
|
name varchar(64),
|
||||||
|
date_created TIMESTAMP default CURRENT_TIMESTAMP
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE song_playlist(
|
||||||
|
playlist_id integer,
|
||||||
|
song_id integer,
|
||||||
|
date_created TIMESTAMP default CURRENT_TIMESTAMP,
|
||||||
|
primary key (playlist_id, song_id),
|
||||||
|
foreign key (playlist_id) references playlist(id),
|
||||||
|
foreign key (song_id) references song(id)
|
||||||
|
);
|
||||||
@ -2,7 +2,9 @@ import DBA
|
|||||||
from components.ErrorDialog import ErrorDialog
|
from components.ErrorDialog import ErrorDialog
|
||||||
|
|
||||||
|
|
||||||
def update_song_in_library(library_id: int, edited_column_name: str, user_input_data: str):
|
def update_song_in_library(
|
||||||
|
library_id: int, edited_column_name: str, user_input_data: str
|
||||||
|
):
|
||||||
"""Updates a field in the library database based on an ID
|
"""Updates a field in the library database based on an ID
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@ -15,10 +17,14 @@ def update_song_in_library(library_id: int, edited_column_name: str, user_input_
|
|||||||
try:
|
try:
|
||||||
with DBA.DBAccess() as db:
|
with DBA.DBAccess() as db:
|
||||||
# yeah yeah this is bad... the column names are defined in the program by me so im ok with it because it works
|
# yeah yeah this is bad... the column names are defined in the program by me so im ok with it because it works
|
||||||
db.execute(f'UPDATE library SET {edited_column_name} = ? WHERE id = ?', (user_input_data, library_id))
|
db.execute(
|
||||||
|
f"UPDATE song SET {edited_column_name} = ? WHERE id = ?",
|
||||||
|
(user_input_data, library_id),
|
||||||
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
dialog = ErrorDialog(f'Unable to update [{edited_column_name}] to [{user_input_data}]. ID: {library_id} | {e}')
|
dialog = ErrorDialog(
|
||||||
|
f"Unable to update [{edited_column_name}] to [{user_input_data}]. ID: {library_id} | {e}"
|
||||||
|
)
|
||||||
dialog.exec_()
|
dialog.exec_()
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user