summaryrefslogtreecommitdiff
path: root/python/database.py
diff options
context:
space:
mode:
Diffstat (limited to 'python/database.py')
-rw-r--r--python/database.py62
1 files changed, 50 insertions, 12 deletions
diff --git a/python/database.py b/python/database.py
index 5bc8b5f..06f5d61 100644
--- a/python/database.py
+++ b/python/database.py
@@ -1,20 +1,58 @@
import mysql.connector
-def getConnector():
- return mysql.connector.connect(
- host="localhost",
- user="root",
+connector = mysql.connector.connect(
+ host="localhost",
+ user="root",
+ database="balalaika",
+)
+
+cursor = connector.cursor()
+
+
+def get_song(id, name):
+ if id is None:
+ id = "%"
+
+ if name is None:
+ name = "%"
+
+ cursor.execute(
+ """
+ select * from song
+ where id like %(id)s
+ and name like %(name)s;
+ """,
+ {
+ 'id': id,
+ 'name': name,
+ }
)
+ return cursor.fetchall()
+
-def getCursor(connector):
- return connector.cursor()
+def get_filtered_songs(song, album, artist):
+ if artist is None:
+ artist = "%"
+ if album is None:
+ album = "%"
+
+ cursor.execute(
+ """
+ select song.name, album.name, artist.name
+ from song
+ inner join album on song.album_id=album.id
+ inner join artist on album.artist_id=artist.id
+ where artist.name like %(artist)s
+ and album.name like %(album)s;
+ """,
+ {
+ 'artist': artist,
+ 'album': album,
+ 'song': song,
+ }
+ )
-def runQuery(connector, query):
- cursor = getCursor(connector)
- cursor.execute(query)
- result = cursor.fetchall()
- cursor.close()
- return result
+ return cursor.fetchall()