· 8 years ago · Aug 02, 2018, 12:42 AM
1package nl.gerhardlinkemeyer.gerhardlinkemeyersoundboard;
2
3import android.content.ContentValues;
4import android.content.Context;
5import android.database.Cursor;
6import android.database.sqlite.SQLiteDatabase;
7import android.database.sqlite.SQLiteOpenHelper;
8import android.util.Log;
9
10import java.util.Arrays;
11import java.util.List;
12
13/**
14 * Created by Farid on 02.08.2018.
15 */
16
17public class DatabaseHandler extends SQLiteOpenHelper {
18
19 private static final String LOG_TAG = "DATABASEHANDLER";
20
21 private static final String DATABASE_NAME = "soundboard.db";
22 private static final int DATABASE_VERSION = 1;
23
24 //Tabelle 1 = Alle Sounds
25 private static final String MAIN_TABLE = "main_table";
26
27 private static final String MAIN_ID = "_id";
28 private static final String MAIN_NAME = "soundName";
29 private static final String MAIN_ITEM_ID = "soundId";
30
31 //Tabelle 2 = Favoriten
32
33 private static final String FAVORITES_TABLE = "favorites_table";
34
35 private static final String FAVORITES_ID = "_id";
36 private static final String FAVORITES_NAME = "favoName";
37 private static final String FAVORITE_ITEM_ID = "favoId";
38
39 private static final String SQL_CREATE_MAIN_TABLE = "CREATE TABLE IF NOT EXISTS " + MAIN_TABLE + "(" + MAIN_ID + " INTEGER PRIMATE KEY AUTOINCREMENT, " + MAIN_NAME + " TEXT, " + MAIN_ITEM_ID + " INTEGER unique);";
40 private static final String SQL_CREATE_FAVORITES_TABLE = "CREATE TABLE IF NOT EXISTS " + FAVORITES_TABLE + "(" + FAVORITES_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + FAVORITES_NAME + " TEXT, " + FAVORITE_ITEM_ID + " INTEGER);";
41
42
43 public DatabaseHandler(Context context) {
44 super(context, DATABASE_NAME, null, DATABASE_VERSION);
45 }
46
47 @Override
48 public void onCreate(SQLiteDatabase db) {
49
50 try {
51
52 db.execSQL(SQL_CREATE_MAIN_TABLE);
53 db.execSQL(SQL_CREATE_FAVORITES_TABLE);
54
55 } catch(Exception e) {
56
57 Log.e(LOG_TAG, "Fehler beim Erstellen der Datenbank: " + e.getMessage());
58 }
59
60 }
61
62 @Override
63 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
64
65 db.execSQL("DROP TABLE IF EXISTS " + MAIN_TABLE);
66 onCreate(db);
67
68 }
69
70 private boolean verification(SQLiteDatabase database, String tableName, String idColumn, Integer soundId) {
71
72 int count = -1;
73 Cursor cursor = null;
74
75 try {
76
77 String query = "SELECT * FROM " + tableName + " WHERE " + idColumn + " = " + soundId;
78 cursor = database.rawQuery(query, null);
79
80 if (cursor.moveToFirst())
81 count = cursor.getInt(0);
82
83 return (count > 0);
84
85 } finally {
86
87 if(cursor != null)
88 cursor.close();
89 }
90 }
91
92 public void createSoundCollection(Context context) {
93
94 List<String> nameList = Arrays.asList(context.getResources().getStringArray(R.array.soundNames));
95
96 SoundObject[] soundItems = {new SoundObject(nameList.get(0), R.raw.au), new SoundObject(nameList.get(1), R.raw.geil), new SoundObject(nameList.get(2), R.raw.huujuju)};
97
98 for(SoundObject i: soundItems)
99 putIntoMain(i);
100 }
101
102 private void putIntoMain (SoundObject soundObject) {
103
104 SQLiteDatabase database = this.getWritableDatabase();
105
106 if(!verification(database, MAIN_NAME, MAIN_ITEM_ID, soundObject.getItemID())) {
107
108 try {
109
110 ContentValues contentValues = new ContentValues();
111
112 contentValues.put(MAIN_NAME, soundObject.getItemName());
113 contentValues.put(MAIN_ITEM_ID, soundObject.getItemID());
114
115 database.insert(MAIN_TABLE, null, contentValues);
116
117 } catch (Exception e) {
118
119 Log.e(LOG_TAG, "(MAIN) Fehler beim Einfügen des Sounds" + e.getMessage());
120 } finally {
121
122 database.close();
123 }
124 }
125 }
126
127 public Cursor getSoundCollection(){
128
129 SQLiteDatabase database = this.getWritableDatabase();
130
131 return database.rawQuery("SELECT * FROM " + MAIN_TABLE + " ORDER BY " + MAIN_NAME, null);
132 }
133
134 public void appUpdate() {
135 }
136}