Add list of played songs

This commit is contained in:
2019-06-29 01:24:54 +02:00
parent 438bcffd0b
commit 5f551ac092
7 changed files with 181 additions and 12 deletions

View File

@ -7,6 +7,7 @@ from io import StringIO
song_table = "songs"
entry_table = "entries"
index_label = "Id"
done_table = "done_songs"
def open_db():
conn = sqlite3.connect("data/test.db")
@ -28,14 +29,19 @@ def import_songs(song_csv):
def create_entry_table():
conn = open_db()
t = (entry_table,)
conn.execute('CREATE TABLE IF NOT EXISTS '+entry_table +
' (ID INTEGER PRIMARY KEY NOT NULL, Song_Id INTEGER NOT NULL, Name VARCHAR(255))')
conn.close()
def create_done_song_table():
conn = open_db()
conn.execute('CREATE TABLE IF NOT EXISTS '+done_table +
' (Song_Id INTEGER PRIMARY KEY NOT NULL, Plays INTEGER)')
conn.close()
def create_song_table():
conn = open_db()
t = (entry_table,)
conn.execute("CREATE TABLE IF NOT EXISTS \""+song_table+"""\" (
"Id" INTEGER,
"Title" TEXT,
@ -52,21 +58,37 @@ def create_song_table():
def create_list_view():
conn = open_db()
conn.execute("""CREATE VIEW IF NOT EXISTS [Liste] AS
SELECT Name, Title, Artist, entries.Id
SELECT Name, Title, Artist, entries.Id, songs.Id
FROM entries, songs
WHERE entries.Song_Id=songs.Id""")
conn.close()
def create_done_song_view():
conn = open_db()
conn.execute("""CREATE VIEW IF NOT EXISTS [Abspielliste] AS
SELECT Artist || \" - \" || Title AS Song, Plays AS Wiedergaben
FROM songs, done_songs
WHERE done_songs.Song_Id=songs.Id""")
conn.close()
def get_list():
conn = open_db()
cur = conn.cursor()
cur.execute("SELECT * FROM Liste")
return cur.fetchall()
def get_played_list():
conn = open_db()
cur = conn.cursor()
cur.execute("SELECT * FROM Abspielliste")
return cur.fetchall()
def get_song_list():
conn =open_db()
cur = conn.cursor()
cur.execute("SELECT Title || \" - \" || Artist AS Song, Id FROM songs")
cur.execute("SELECT Artist || \" - \" || Title AS Song, Id FROM songs")
return cur.fetchall()
def get_song_completions(input_string):
@ -86,6 +108,30 @@ def add_entry(name,song_id):
conn.close()
return
def add_sung_song(entry_id):
conn = open_db()
cur = conn.cursor()
cur.execute("""SELECT Song_Id FROM entries WHERE Id=?""",(entry_id,))
song_id = cur.fetchone()[0]
cur.execute("""INSERT OR REPLACE INTO done_songs (Song_Id, Plays)
VALUES("""+str(song_id)+""",
COALESCE(
(SELECT Plays FROM done_songs
WHERE Song_Id="""+str(song_id)+"), 0) + 1)"
)
conn.commit()
delete_entry(entry_id)
conn.close()
return True
def clear_played_songs():
conn = open_db()
cur = conn.cursor()
cur.execute("DELETE FROM done_songs")
conn.commit()
conn.close()
return True
def delete_entry(id):
conn = open_db()
cur = conn.cursor()