In my app I want a function that will return all data from the database so that this can be backed up online. I have built this from scratch so there be be an easier way I do not know about. When the function runs, each table is looped through and the data is stored in an array. This works for the first two loops but the third loop (table) doesn't work. This table should be empty as it is only used in another part of the app that has not yet been set up. I have been on for two days rewriting this function and clearing the databases but I am no further forward. I really cant see what is wrong here. This is my function:
public List backUpData(){
List allData = new ArrayList();
List<HashMap<String, String>> users = new ArrayList<HashMap<String, String>>();
List<HashMap<String, String>> tricks = new ArrayList<HashMap<String, String>>();
List<HashMap<String, String>> saved = new ArrayList<HashMap<String, String>>();
SQLiteDatabase db = this.getReadableDatabase();
String queryUsers = "SELECT * FROM " + TABLE_USER;
String queryTricks = "SELECT * FROM " + TABLE_STATS;
String querySaved = "SELECT * FROM " + TABLE_TRICKS;
int x = 0;
Cursor cursor = db.rawQuery(queryUsers, null);
if(cursor.getCount() > 0) {
cursor.moveToFirst();
while(!cursor.isAfterLast()){
HashMap<String, String> user = new HashMap<String, String>();
user.put(KEY_ID, cursor.getString(0));
user.put(USER_NAME, cursor.getString(1));
users.add(x, user);
x++;
cursor.moveToNext();
}
cursor.close();
x = 0;
}
Cursor cSaved = db.rawQuery(querySaved, null);
if(cSaved .getCount() > 0) {
Log.d("SAVED","Has " + cSaved .getCount() + " saved tricks");
cSaved .moveToFirst();
while (!cSaved .isAfterLast()) {
HashMap<String, String> trick = new HashMap<String, String>();
trick.put(KEY_ID, cSaved .getString(0));
trick.put(TRICK_NAME, cSaved .getString(1));
tricks.add(x, trick);
x++;
cSaved .moveToNext();
}
cSaved .close();
x = 0;
}
Log.d("ERROR","is after here");
//repeat for stats
Cursor cTricks = db.rawQuery(queryTricks, null);
if(cTricks.getCount() > 0){
Log.d("STATS","this should be 0 but is " + cTricks.getCount());
cTricks.moveToFirst();
while(!cTricks.isAfterLast()){
HashMap<String, String> trick = new HashMap<String, String>();
Log.d("COLS", cTricks.toString());
trick.put(KEY_ID, cTricks.getString(0));
trick.put(PLAYER_ID, cTricks.getString(1));
trick.put(TRICK_NAME, cTricks.getString(2));
trick.put(LANDED, cTricks.getString(3));
trick.put(BAILED, cTricks.getString(4));
trick.put(CALLED, cTricks.getString(5));
trick.put(USER_NAME, cTricks.getString(6));
tricks.add(x, trick);
x++;
cTricks.moveToNext();
}
cTricks.close();
}
allData.add(0, users);
allData.add(1, saved);
allData.add(2, tricks);
return allData;
}
And this taken from my log:
09-15 10:00:21.382: D/ERROR(15169): is after here
09-15 10:00:21.383: D/STATS(15169): this should be 0 but is 1
09-15 10:00:21.383: D/COLS(15169): android.database.sqlite.SQLiteCursor@34f363b2
09-15 10:00:21.383: E/CursorWindow(15169): Failed to read row 0, column 2 from a CursorWindow which has 1 rows, 2 columns.
09-15 10:00:21.384: D/AndroidRuntime(15169): Shutting down VM
09-15 10:00:21.386: E/AndroidRuntime(15169): FATAL EXCEPTION: main
09-15 10:00:21.386: E/AndroidRuntime(15169): Process: r1d.org.uk.oiaskatedice, PID: 15169
09-15 10:00:21.386: E/AndroidRuntime(15169): java.lang.RuntimeException: Unable to start activity ComponentInfo{r1d.org.uk.oiaskatedice/r1d.org.uk.oiaskatedice.MainActivity}: java.lang.IllegalStateException: Couldn't read row 0, col 2 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
09-15 10:00:21.386: E/AndroidRuntime(15169): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2345)
09-15 10:00:21.386: E/AndroidRuntime(15169): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2405)
09-15 10:00:21.386: E/AndroidRuntime(15169): at android.app.ActivityThread.access$800(ActivityThread.java:149)
09-15 10:00:21.386: E/AndroidRuntime(15169): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1324)
09-15 10:00:21.386: E/AndroidRuntime(15169): at android.os.Handler.dispatchMessage(Handler.java:102)
09-15 10:00:21.386: E/AndroidRuntime(15169): at android.os.Looper.loop(Looper.java:211)
09-15 10:00:21.386: E/AndroidRuntime(15169): at android.app.ActivityThread.main(ActivityThread.java:5333)
09-15 10:00:21.386: E/AndroidRuntime(15169): at java.lang.reflect.Method.invoke(Native Method)
09-15 10:00:21.386: E/AndroidRuntime(15169): at java.lang.reflect.Method.invoke(Method.java:372)
09-15 10:00:21.386: E/AndroidRuntime(15169): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1016)
09-15 10:00:21.386: E/AndroidRuntime(15169): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)
09-15 10:00:21.386: E/AndroidRuntime(15169): Caused by: java.lang.IllegalStateException: Couldn't read row 0, col 2 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
09-15 10:00:21.386: E/AndroidRuntime(15169): at android.database.CursorWindow.nativeGetString(Native Method)
09-15 10:00:21.386: E/AndroidRuntime(15169): at android.database.CursorWindow.getString(CursorWindow.java:438)
09-15 10:00:21.386: E/AndroidRuntime(15169): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
09-15 10:00:21.386: E/AndroidRuntime(15169): at LocalDatabase.DatabaseHandler.backUpData(DatabaseHandler.java:133)
09-15 10:00:21.386: E/AndroidRuntime(15169): at r1d.org.uk.backup.backupdata.<init>(backupdata.java:53)
09-15 10:00:21.386: E/AndroidRuntime(15169): at r1d.org.uk.oiaskatedice.MainActivity.onCreate(MainActivity.java:41)
09-15 10:00:21.386: E/AndroidRuntime(15169): at android.app.Activity.performCreate(Activity.java:5933)
09-15 10:00:21.386: E/AndroidRuntime(15169): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
09-15 10:00:21.386: E/AndroidRuntime(15169): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
09-15 10:00:21.386: E/AndroidRuntime(15169): ... 10 more
I may be wrong here but I take this line "Couldn't read row 0, col 2 from CursorWindow" to mean that there id no column 2, however in this table there are 6 columns.
Aucun commentaire:
Enregistrer un commentaire