简体   繁体   中英

Reading from SQLITE in Android always returns 0 rows

I am attempting my first use of Sqlite in Android with mixed results.

In my first activity I load a number of rows into a table using code like this

public void addTableStatus(String tableId, String name) {

    Log.d("DB", "Before Insert :: " + "Id=[" + tableId + "]");

    if (sd == null) {
        Log.d("DB", "Get Database");
        sd = getWritableDatabase();
    }

    ContentValues cv = new ContentValues();
    cv.put(V2VocabsTable.ID, tableId);
    cv.put(V2VocabsTable.NAME, name);
    cv.put(V2VocabsTable.LOADED, "NO");

    long result = sd.insert(V2VocabsTable.TABLE_NAME, null, cv);

    Log.d("DB", "After Insert :: " + result);

    String query = "SELECT * from " + V2VocabsTable.TABLE_NAME ;

    Log.i("DB2", "Query    : " + query);
    Cursor mCursor = sd.rawQuery(query, null);
    Log.i("DB2", "Result : " + mCursor.getCount());
}

The log file shows data going in as follows :

01-13 21:54:09.788: D/DB(22744): Before Insert :: Id=[ZU055]
01-13 21:54:09.798: D/DB(22744): After Insert :: 129
01-13 21:54:09.798: I/DB2(22744): Query    : SELECT * from v2TableStatus
01-13 21:54:09.798: I/DB2(22744): Result : 129
01-13 21:54:09.798: D/DB(22744): ZU055 :: WaitingListType-v1.0.xml
01-13 21:54:09.798: D/DB(22744): Before Insert :: Id=[ZU056]
01-13 21:54:09.798: D/DB(22744): After Insert :: 130
01-13 21:54:09.798: I/DB2(22744): Query    : SELECT * from v2TableStatus
01-13 21:54:09.798: I/DB2(22744): Result : 130
01-13 21:54:09.798: D/DB(22744): ZU056 :: DCRConsentToShareIndicator-v1.0.xml

However when I try to read the table in another activity there does not seem to be any data in the table. I execute the same query (as a test) and always get 0 rows.

public void test(String tableName) {

    if (sd == null) {
        sd = getWritableDatabase();
    }

    tableName = tableName.substring(5).trim();

    String query;

    Log.i("DB_", "Test for : [" + tableName + "]");

    query = "SELECT * from " + V2VocabsTable.TABLE_NAME ;

    Log.i("DB2", "Query    : " + query);
    Cursor mCursor = sd.rawQuery(query, null);
    Log.i("DB2", "Result : " + mCursor.getCount());
}


01-13 21:54:18.838: I/DB_(22744): Test for : [0136]
01-13 21:54:18.838: I/DB2(22744): Query    : SELECT * from v2TableStatus
01-13 21:54:18.838: I/DB2(22744): Result : 0

My mistake, my dbHelper class was erroneously clearing the tables down when it initiated. So when it initiated in the second activity it removed all of the content placed in it from the first activity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM