karaoqueue/database.py

101 lines
2.5 KiB
Python

# -*- coding: utf_8 -*-
import sqlite3
import pandas
from io import StringIO
song_table = "songs"
entry_table = "entries"
index_label = "Id"
def open_db():
conn = sqlite3.connect("test.db")
conn.execute('PRAGMA encoding = "UTF-8";')
return conn
def import_songs(song_csv):
df = pandas.read_csv(StringIO(song_csv), sep=';')
conn = open_db()
df.to_sql(song_table, conn, if_exists='replace',
index=False)
conn.close()
print("Imported songs")
return
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_song_table():
conn = open_db()
t = (entry_table,)
conn.execute("CREATE TABLE IF NOT EXISTS \""+song_table+"""\" (
"Id" INTEGER,
"Title" TEXT,
"Artist" TEXT,
"Year" INTEGER,
"Duo" INTEGER,
"Explicit" INTEGER,
"Date Added" TEXT,
"Styles" TEXT,
"Languages" TEXT
)""")
conn.close()
def create_list_view():
conn = open_db()
conn.execute("""CREATE VIEW IF NOT EXISTS [Liste] AS
SELECT Name, Title, Artist, entries.Id
FROM entries, songs
WHERE entries.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_song_list():
conn =open_db()
cur = conn.cursor()
cur.execute("SELECT Title || \" - \" || Artist AS Song, Id FROM songs")
return cur.fetchall()
def get_song_completions(input_string):
conn = open_db()
cur = conn.cursor()
# Don't look, it burns...
cur.execute(
"SELECT Title || \" - \" || Artist AS Song, Id FROM songs WHERE Song LIKE REPLACE(REPLACE(REPLACE(REPLACE(UPPER('%"+input_string+"%'),'ö','Ö'),'ü','Ü'),'ä','Ä'),'ß','')")
return cur.fetchall()
def add_entry(name,song_id):
conn = open_db()
cur = conn.cursor()
cur.execute(
"INSERT INTO entries (Song_Id,Name) VALUES(?,?);", (song_id,name))
conn.commit()
conn.close()
return
def delete_entry(id):
conn = open_db()
cur = conn.cursor()
cur.execute("DELETE FROM entries WHERE id=?",(id,))
conn.commit()
conn.close()
return True
def delete_all_entries():
conn = open_db()
cur = conn.cursor()
cur.execute("DELETE FROM entries")
conn.commit()
conn.close()
return True