· 6 years ago · Jun 24, 2019, 09:14 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 DataBase2 extends DBManager {
14 private static SQLiteDatabase dbw = null;
15 private static String VU_ARTIST_TABLE = "artists";
16 private static String VU_ALBUM_TABLE = "albums";
17 private static Context context;
18
19
20 public DataBase2(Context context) {
21 super(context, "vu2.db", 1);
22 this.context = context;
23
24 }
25
26 @Override
27 public void onCreate(SQLiteDatabase db) {
28 String artist_table = "CREATE TABLE IF NOT EXISTS " + VU_ARTIST_TABLE
29 + " (ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name VARCHAR(155))";
30 String album_table = "CREATE TABLE IF NOT EXISTS " + VU_ALBUM_TABLE
31 + " (ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name VARCHAR(155))";
32 db.execSQL(artist_table);
33 db.execSQL(album_table);
34 //db.execSQL("INSERT into artist_table('col_artist_name') Values('ausaf')");
35 //db.execSQL("INSERT into artist_table('col_artist_name') Values('ausaf')");
36 //db.execSQL("INSERT into artist_table('col_artist_name') Values('ausaf')");
37 //db.execSQL("INSERT into artist_table('col_artist_name') Values('ausaf')");
38
39 }
40
41 @Override
42 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
43 //onCreate(db);
44 Log.i("Shiffu", "Upgrading Database");
45 }
46
47 private void connect(){
48 if(null == dbw) {
49 dbw = getDb();
50 Log.i("Shiffu", "Connecting to db");
51 }
52 }
53
54 private boolean Close() {
55 dbw.setTransactionSuccessful();
56 dbw.endTransaction();
57 return true;
58 }
59
60 public void addArtist(String artist){
61 connect();
62 //dbw.execSQL("INSERT into artist_table('col_artist_name') Values(" + artist + ")");
63 dbw.beginTransaction();
64 ContentValues contentValues = new ContentValues();
65 contentValues.put("name",artist);
66 try{
67 long xya = dbw.insertOrThrow(VU_ARTIST_TABLE,null,contentValues);
68 Log.i("Shiffu","done: " + xya);
69 }catch(SQLException e){
70 Log.i("Shiffu", e.getMessage());
71 }
72 Close();
73 }
74
75 public List fetchAllArtist() {
76 connect();
77 dbw.beginTransaction();
78 List ausaf = new ArrayList();
79 String all = "SELECT * FROM " + VU_ARTIST_TABLE;
80 Cursor cursor = dbw.rawQuery(all, null);
81 int ar_int = cursor.getColumnIndex("name");
82 if (cursor.moveToFirst()) {
83 do {
84 String art_name = cursor.getString(ar_int);
85 ausaf.add(art_name);
86 } while (cursor.moveToNext());
87 }
88 close();
89 return ausaf;
90 }
91
92 public void addAlbum(String album){
93 connect();
94 dbw.beginTransaction();
95 ContentValues contentValues = new ContentValues();
96 contentValues.put("col_album_name",album);
97 try{
98 dbw.insert(VU_ALBUM_TABLE,null,contentValues);
99 Log.i("here","done");
100 }catch(SQLException e){
101 Log.i("Shiffu", e.getMessage());
102 }
103 Close();
104 }
105
106 public List fetchAllAlbum() {
107 connect();
108 //
109 dbw.beginTransaction();
110 List ausaf = new ArrayList();
111 String all = "SELECT * FROM " + VU_ALBUM_TABLE;
112 Cursor cursor = dbw.rawQuery(all, null);
113 int ar_int = cursor.getColumnIndex("name");
114 if (cursor.moveToFirst()) {
115 do {
116 String art_name = cursor.getString(ar_int);
117 ausaf.add(art_name);
118 } while (cursor.moveToNext());
119 }
120 close();
121 return ausaf;
122 }
123
124 public void filter(String title,String artist, String album,String genre,String tempo,String year){
125
126 }
127}