· 7 years ago · Feb 09, 2019, 07:08 PM
1package se.maxallan.birdsound;
2
3 import android.content.ContentValues;
4 import android.content.Context;
5 import android.database.Cursor;
6 import android.database.SQLException;
7 import android.database.sqlite.SQLiteDatabase;
8 import android.database.sqlite.SQLiteOpenHelper;
9 import android.util.Log;
10
11 public class SpeciesDbAdapter extends SQLiteOpenHelper{
12 public static final String DB_SPECIE_ID = "_id";
13 public static final String DB_SCF_NAME = "scf_name";
14
15 public static final String DB_T_LANG = "lang";
16 public static final String DB_T_TRANS = "translated";
17
18 private static SpeciesDbAdapter mDbHelp;
19 private static SQLiteDatabase mDb;
20
21 private static final String DB_NAME = "database.db";
22 private static final String DB_TBL_SPECIES = "species";
23 private static final String DB_TBL_TRANSLATIONS = "translations";
24 //private static final String DB_TBL_SOUNDS = "sounds";
25 private static final int DB_VERSION = 1;
26
27 private final Context mCtx;
28
29 static String createSpecies = "CREATE TABLE if not exists "
30 + DB_TBL_SPECIES
31 +" ("+DB_SPECIE_ID +" INTEGER PRIMARY KEY AUTOINCREMENT,"
32 +DB_SCF_NAME+");";
33
34 static String createTranslations = "CREATE TABLE if not exists "
35 + DB_TBL_TRANSLATIONS
36 +" (tid INTEGER PRIMARY KEY AUTOINCREMENT,"
37 +DB_SPECIE_ID+","
38 +DB_T_TRANS+","
39 +DB_T_LANG+");"; //The error is here?
40
41 public SpeciesDbAdapter(Context context) {
42 super(context, DB_NAME, null, DB_VERSION);
43 this.mCtx = context;
44 }
45 @Override
46 public void onCreate(SQLiteDatabase db) {
47 db.execSQL(createSpecies);
48 db.execSQL(createTranslations);
49 }
50 @Override
51 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
52 }
53
54 //Open the database
55 public SpeciesDbAdapter open() throws SQLException {
56 mDbHelp = new SpeciesDbAdapter(mCtx);
57 mDb = mDbHelp.getWritableDatabase();
58 return this;
59 }
60 //Close the connection
61 public void close() {
62 if (mDbHelp != null) {
63 mDbHelp.close();
64 }
65 }
66
67 public void addSpecie(String scf, String translation, String lang){
68 ContentValues specieValues = new ContentValues();
69 ContentValues translationsValues = new ContentValues();
70 specieValues.put(DB_SCF_NAME, scf);
71 int LastInsertedId = (int) mDb.insert(DB_TBL_SPECIES, null, specieValues);
72 translationsValues.put(DB_T_LANG, lang);
73 translationsValues.put(DB_T_TRANS, translation);
74 translationsValues.put(DB_SPECIE_ID, LastInsertedId);
75 mDb.insert(DB_TBL_TRANSLATIONS, null, translationsValues);
76 }
77
78 public Cursor fetchAllSpecies() {
79 Cursor mCursor = mDb.rawQuery("select * from "+ DB_TBL_SPECIES +" s inner join "+ DB_TBL_TRANSLATIONS +" t on s._id=t._id", null);
80 Log.d("Database", "Result cursor size " + mCursor.getCount() );
81 if (mCursor != null) {
82 mCursor.moveToFirst();
83 }
84 return mCursor;
85 }
86 public void insertSomeSpecies() {
87 addSpecie("Cygnus olor", "Knölsvan", "sv");
88 addSpecie("Cygnus cygnus", "SÃ¥ngsvan", "sv");
89 }
90
91 }
92
93@Override
94public void onCreate(SQLiteDatabase db) {
95 db.execSQL(createSpecies);
96}
97
98db.execSQL(createTranslations);
99
100private static final int DB_VERSION = 2;
101
102@Override
103public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
104 db.execSQL("DROP TABLE IF EXISTS " + DB_TBL_SPECIES);
105 db.execSQL("DROP TABLE IF EXISTS " + DB_TBL_TRANSLATIONS);
106 onCreate(db);
107}