· 6 years ago · Jun 20, 2019, 08:50 AM
1package com.ausafali.beats;
2
3import android.content.ContentValues;
4import android.content.Context;
5import android.database.Cursor;
6import android.database.SQLException;
7import android.database.sqlite.SQLiteDatabase;
8import android.util.Log;
9
10import java.util.ArrayList;
11import java.util.List;
12
13public class VUDB extends DBManager {
14 private static final int DATABASE_VERSION = 2;
15 private static final String DATABASE_NAME = "vu.db";
16 private static Context context;
17 private static final String VU_TABLE_NAME = "songs";
18 private static final String VU_ARTIST_TABLE = "artists";
19 private static final String VU_ALBUM_TABLE = "album";
20 private static SQLiteDatabase dbw = null;
21
22 public VUDB(Context context) {
23 super(context, DATABASE_NAME, DATABASE_VERSION);
24 this.context = context;
25 }
26
27 @Override
28 public void onCreate(SQLiteDatabase db) {
29 Log.i("MasterShiffu", "Creating db");
30 String VU_TABLE = "CREATE TABLE IF NOT EXISTS " + VU_TABLE_NAME
31 + " (ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, title VARCHAR(255) UNIQUE, artist VARCHAR(255),"
32 + " year INT(4), album VARCHAR(255),genre VARCHAR(255), tempo VARCHAR(255),location VARCHAR(255),thumb VARCHAR(255))";
33 String artist_table = "CREATE TABLE IF NOT EXISTS " + VU_ARTIST_TABLE
34 + " (ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, col_artist_name VARCHAR(255))";
35 String album_table = "CREATE TABLE IF NOT EXISTS " + VU_ALBUM_TABLE
36 + " (ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, album_name VARCHAR(255))";
37 db.execSQL(VU_TABLE);
38 db.execSQL(artist_table);
39 db.execSQL(album_table);
40 }
41
42 @Override
43 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
44 onCreate(db);
45
46 }
47
48 private void connect(){
49 Log.i("Shiffu", "Connecting to db");
50 if(null == dbw) {
51 dbw = getDb();
52 }
53 }
54
55 private boolean Close() {
56 dbw.setTransactionSuccessful();
57 dbw.endTransaction();
58 return true;
59 }
60
61 public void addSong(String title, String artist, Integer year, String album, String genre, String tempo, String location, String thumb){
62 connect();
63 ContentValues contentValues = new ContentValues();
64 contentValues.put("title",title);
65 contentValues.put("artist",artist);
66 contentValues.put("year", year);
67 contentValues.put("album",album);
68 contentValues.put("genre", genre);
69 contentValues.put("tempo", tempo);
70 contentValues.put("location", location);
71 contentValues.put("thumb", thumb);
72 dbw.insert(VU_TABLE_NAME,null,contentValues);
73 Close();
74 }
75
76 public List filter(String artist, String genre, String album, String tempo, String year) {
77 connect();
78 dbw.beginTransaction();
79 List songlist2 = new ArrayList( );
80 Cursor cursor = (Cursor) dbw.rawQuery("select * from table_music where artist_name LIKE '%" + artist + "%' AND genre LIKE '%" + genre + "%' AND album_name LIKE '%" +
81 album + "%' AND tempo LIKE '%" + tempo + "%' AND year_of_album LIKE '%" + year + "%'" ,null);
82 int songindex = cursor.getColumnIndex("song_title");
83 int artistindex = cursor.getColumnIndex("artist_name");
84 int genreindex = cursor.getColumnIndex("genre");
85 int albumindex = cursor.getColumnIndex("album_name");
86 int tempoindex = cursor.getColumnIndex("tempo");
87 int yearindex = cursor.getColumnIndex("year_of_album");
88 int location = cursor.getColumnIndex("song_path");
89
90 if (cursor.moveToFirst())
91 do {
92 String title1 = cursor.getString(songindex);
93 String arstist = cursor.getString( artistindex );
94 String genre1= cursor.getString(genreindex);
95 String album1 = cursor.getString(albumindex);
96 String tempo1 = cursor.getString(tempoindex);
97 String year1 = Integer.toString( cursor.getInt( yearindex ) );
98 String songPath = cursor.getString(location);
99 //songlist2.add(title1 + "@@@@" + arstist + "@@@@" + genre + "@@@@" + album + "@@@@" + tempo + "@@@@" + year + "@@@@" + songPath);
100 songlist2.add("Title: " + title1 + "\n" + "Artist: " + arstist + "\n" + "Genre: " + genre1 + "\n" + " Album: " + album1 + "\n" + "Tempo: " + tempo1 + "\n" + "Year: "+ year1 + "\n" + songPath);
101 }while (cursor.moveToNext());
102 Close();
103 return songlist2;
104 }
105
106 public void addArtist(String artist){
107 connect();
108 dbw.beginTransaction();
109 ContentValues contentValues = new ContentValues();
110 contentValues.put("col_artist_name",artist);
111 try{
112 dbw.insert(VU_ARTIST_TABLE,null,contentValues);
113 }catch(SQLException e){
114 Log.i("Shiffu", e.getMessage());
115 }
116 Close();
117 }
118
119 public void addAlbum(String album){
120 connect();
121 dbw.beginTransaction();
122 ContentValues contentValues = new ContentValues();
123
124 contentValues.put("album",album);
125 dbw.insert(VU_ALBUM_TABLE,null,contentValues);
126 Close();
127 }
128
129 public List fetchAllArtist(){
130 connect();
131 dbw.beginTransaction();
132 List ausaf = new ArrayList();
133 String all = "Select * from artists";
134 Cursor cursor = dbw.rawQuery(all,null);
135 int ar_int = cursor.getColumnIndex("col_artist_name");
136 if (cursor.moveToFirst()){
137 do {
138 String art_name = cursor.getString(ar_int);
139 ausaf.add(art_name);
140 } while (cursor.moveToNext());
141 }
142 close();
143 return ausaf;
144 }
145
146 public List fetchAllAlbum(){
147 connect();
148 dbw.beginTransaction();
149 List ausaf = new ArrayList();
150 String all = "Select * from album";
151 Cursor cursor = dbw.rawQuery(all,null);
152 int ar_int = cursor.getColumnIndex("album_name");
153 if (cursor.moveToFirst()){
154 do {
155 String art_name = cursor.getString(ar_int);
156 ausaf.add(art_name);
157 } while (cursor.moveToNext());
158 }
159 close();
160 return ausaf;
161 }
162}