vendredi 6 mai 2016

How to access the same database from multiple activities in SQLite android

I am working on an app where user can upload a new file from mobile and the data will be loaded to database or the user can open previously loaded data from database. This is my first activity. I have a database already created with loaded data etc but when my app first opens and when the user clicks on the button "Open Previous data" I must load the previously available data from database.

Here is my database class,

NewDbHelper.java

public class NewDbHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "crosswordpuzzledb.db";
public static final String TABLE_NAME = "puzzl_table";

public static final String id = "ID";
public static final String C1 = "ANSWER_NUMBER";
public static final String C2 = "ANSWER_VALUES";

public static final String C3 = "CLUES_NUMBERS";
public static final String C4 = "CLUES_VALUES";
private String tag;
static SQLiteDatabase db;
ArrayList<String> values;
public NewDbHelper(Context context) {
    super(context, DATABASE_NAME, null, 1);
    SQLiteDatabase db = this.getWritableDatabase(); //Create and/or open a database that will be used for reading and writing. The first time this is called, the database will be opened and onCreate, onUpgrade and/or onOpen will be called.
  //  db.execSQL("delete from "+ TABLE_NAME);
}

@Override
public void onCreate(SQLiteDatabase db) {
 db.execSQL("create table " + TABLE_NAME + " (ID INTEGER PRIMARY KEY , ANSWER_NUMBER INTEGER, ANSWER_VALUES TEXT, CLUES_NUMBERS INTEGER , CLUES_VALUES TEXT)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS" + TABLE_NAME);
    onCreate(db);
}

public boolean InsertVal(int ansnum, String ansval, int cluenum, String clueval) { // these are the arguements
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(C1, ansnum);
    contentValues.put(C2, ansval);
    contentValues.put(C3, cluenum);
    contentValues.put(C4, clueval);


    long result = db.insertWithOnConflict(TABLE_NAME, null, contentValues, SQLiteDatabase.CONFLICT_REPLACE);
    if (result == -1) {
        return false;

    } else {
        return true;
    }
}
    public ArrayList<String> getValues() {
    values = new ArrayList<String>();

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery("SELECT CLUES_VALUES FROM " + TABLE_NAME, null);

    if (cursor.moveToFirst()) {
        do {
            values.add(cursor.getString(cursor.getColumnIndex("CLUES_VALUES")));
        } while (cursor.moveToNext());
    }

    cursor.close();
    db.close();
    for (String val : values) {
        Log.d(tag, "clueval " + val);


    }

}

I tried accessing this "getValues" method in the activity where I need to display the available values from the database by using the below code .

RecentLoad.java

   NewDbHelper mydb;
   mydb = new NewDbHelper(this);
    ArrayList<String> d = new ArrayList<String>();

    d = mydb.getValues();
    for (String disp : d) {
        Log.d(tag, "display");
        Log.d(tag, "values" + disp);
    }

I tried running this but its not fetching the values from already created database. This class will trigger when the button "load previous data" is clicked . I tried looking at answers in stack overflow but nothing helped . Any help would be great !!

Aucun commentaire:

Enregistrer un commentaire