0

I am having a problem with SQLiteOpenHelper Class. I insert a row into the table and then try to read the data, I get nothing at the time (right after inserting the data). I only get the information when the activity is closed and then reopened.

MyDB db = new MyDB(context);
db.insert(string1, string2, string3, string4);

Now in the next code row, if I try to read what I inserted:

List<String> list;
list=db.loadHandler();

list is empty until I close the activity and open it again. Why it is?

class MyDB extends SQLiteOpenHelper {

private static final int VERSION_BASEDATOS = 1;

private static final String NAME = "checkList.db";

private static final String TABLA_CHECK_LIST = "CREATE TABLE IF NOT EXISTS checkList" +
        "(id INTEGER PRIMARY KEY AUTOINCREMENT, file TEXT, se TEXT, date TEXT, aux TEXT)";


public MyDB(Context context) {
    super(context, NAME, null, VERSION_BASEDATOS);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(TABLA_CHECK_LIST);
}

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

public boolean deleteDB(Context context){
    return context.deleteDatabase(NAME);
}


public void insert(String file, String se, String date, String aux) {
    SQLiteDatabase db = getWritableDatabase();
    if(db != null){
        ContentValues valores = new ContentValues();
        valores.put("file", file);
        valores.put("se", se);
        valores.put("date", date);
        valores.put("aux", aux);
        db.insert("checkList", null, valores);
        db.close();
    }
}

public List<String> loadHandler() {
    List<String> list = new ArrayList<String>();
    String query = "Select*FROM checkList";
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(query, null);
    while (cursor.moveToNext()) {
        int result_0 = cursor.getInt(0);
        String result_1= cursor.getString(1);
        String result_2 = cursor.getString(2);
        String result_3=cursor.getString(3);
        String result_4=cursor.getString(4);

        list.add(result_1+"%:&"+result_2+"%:&"+result_3+"%:&"+result_4);

    }
    cursor.close();
    db.close();
    return list;
}

public int getRowCount(){
    String query = "Select*FROM checkList";
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(query, null);
    return cursor.getCount();
}

public void deleteFirstRow()
{
    SQLiteDatabase db = this.getWritableDatabase();
    String where = "id < (select id from checkList order by id limit 15, 1)";
    db.delete("checkList", where, null);
}

public void orderIt(){
    SQLiteDatabase db = this.getWritableDatabase();
    db.execSQL("VACUUM");
}


public String getLastHash(){
    SQLiteDatabase db = this.getWritableDatabase();
    String query = "Select*FROM checkList";
    Cursor cursor = db.rawQuery(query, null);
    cursor.moveToLast();
    return cursor.getString(1);
}

}
Julián Oviedo
  • 564
  • 5
  • 19

1 Answers1

1

Try not to close DB your after operations with it (you do that in two methods - insert and loadHandler):

db.close(); // try to remove this

You can read about closing database in answer to this post

sergiy tikhonov
  • 4,961
  • 1
  • 10
  • 27