Skip to content

Python Simple Music App

music.app

import sqlite3
import time

class Music():
  def __init__(self,song_name,song_singer,song_album,song_company,song_duration):
    self.song_name      = song_name
    self.song_singer    = song_singer
    self.song_album     = song_album
    self.song_company   = song_company
    self.song_duration  = song_duration

  def __str__(self):
    return """
    Song Name     : {}
    Song Singer   : {}
    Song Album    : {}
    Song Company  : {}
    Song Duration : {}
    """.format(self.song_name,self.song_album,self.song_album,self.song_company,self.song_duration)


class MusicApp():
  def __init__(self):
    self.connect_data()

  def connect_data(self):
    self.connect = sqlite3.connect('musicAppData.db')
    self.cursor  = self.connect.cursor()
    query = "CREATE TABLE IF NOT EXISTS musics (song_name TEXT, song_singer TEXT, song_album TEXT, song_company TEXT, song_duration INT)"
    self.cursor.execute(query)
    self.connect.commit()

  def close_data(self):
    self.connect.close()

  def show_musics(self):
    query = "SELECT * FROM musics"
    self.cursor.execute(query)
    all_musics = self.cursor.fetchall()
    if(len(all_musics) == 0):
      print("No songs.")
    else:
      for i in all_musics:
        music = Music(i[0],i[1],i[2],i[3],i[4])
        print(music)

  def music_query(self,song_name):
    query = "SELECT * FROM WHERE song_name = ?"
    self.cursor.execute(query,(song_name,))
    all_musics = self.cursor.fetchall()
    if( len(all_musics) == 0 ):
      print("There is no such music.")
    else:
      music = Music(all_musics[0][0],all_musics[0][1],all_musics[0][2],all_musics[0][3],all_musics[0][4])
      print(music)


  def add_music(self,music):
    query = "INSERT INTO musics VALUES(?,?,?,?,?)"
    self.cursor.execute(query,(music.song_name,music.song_singer,music.song_album,music.song_company,music.song_duration))
    self.connect.commit()

  def delete_music(self,song_name):
    query = "DELETE FROM musics WHERE song_name = ?"
    self.cursor.execute(query,(song_name,))
    self.connect.commit()

  def add_song_duration(self,song_name):
    query = "SELECT * FROM musics WHERE song_name = ?"
    self.cursor.execute(query)
    all_musics = self.cursor.fetchall()
    if(len(all_musics) == 0):
      print("There is no such music.")
    else:
      song_duration = all_musics[0][4]
      song_duration += 1
      query2 = "UPDATE musics SET song_duration = ? WHERE song_name = ?"
      self.cursor.execute(query2, (song_duration,song_name))
      self.connect.commit()

music_execute.py

from music import *

print("""***********************************

Welcome to MusicApp Program.

Process;

1. Show All Musics

2. Query Music

3. Add Music

4. Delete Music 

5. Add Music Duraction

Press 'q' to exit.
***********************************""")

musicApp = MusicApp()

while True:
  process = input('Process: ')

  if(process == 'q'):
    print('Music Application Closed')

  elif(process == '1'):
    musicApp.show_musics()

  elif(process == '2'):
    song_name = input('Song Name: ')
    time.sleep(2)
    musicApp.music_query(song_name)

  elif(process == '3'):
    Song_Name = input('Song Name: ')
    Song_Singer = input('Song Singer: ')
    Song_Album = input('Song Album: ')
    Song_Company = input('Song Company: ')
    Song_Duraction = input('Song Duraction')

    new_music = Music(Song_Name,Song_Singer,Song_Album,Song_Company,Song_Duraction)
    musicApp.add_music(new_music)
    time.sleep(1)
    print('music added')


  elif(process == '4'):
    song_name = input('Song Name: ')
    musicApp.delete_music(song_name)
    time.sleep(1)
    print('music deleted')

  elif(process == '5'):
    song_name = input('Song Name: ')
    musicApp.add_song_duration(song_name)
    time.sleep(1)
    print('music add duration')

  else:
    print('Invalid Transaction ...')

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: