Today I wanted to implement a simple SQLite database into my messaging app that should take care of saving messages, unfortunately I am not able to read back the data from the Database and display them into a ListView.
I looked into the database via ADB on my emulator and I saw that the data is saved correctly.
Here is my code
Initialisation of the TABLE with a empty column
db.execSQL("CREATE TABLE IF NOT EXISTS Messages(zero);");
Creating of a column with the chat contact as the column name
public void addMessagetoDB(String message, String from) {
Toast.makeText(this.getApplicationContext(), "msg msg", Toast.LENGTH_LONG).show();
try
{
db.execSQL("ALTER TABLE Messages ADD COLUMN '" + from + "' TEXT");
}
catch (android.database.sqlite.SQLiteException e)
{
}
db.execSQL("INSERT INTO Messages ('" + from + "') VALUES('" + message + "');");
}
Here I can also see that SQLite recognizes my column names, because when I send multiple messages to my client I get the debug messages
E/SQLiteLog﹕ (1) duplicate column name: test@zapp.org
This is my readout which won't work
public void updateView(String from) {
Cursor crs = zapp.rawQuery("SELECT * FROM Messages", null);
while (crs.moveToNext()) {
String name = crs.getString(crs.getColumnIndex(from));
messages.add(name);
}
arrayAdapter.notifyDataSetChanged();
}
This is my exception
06-05 22:15:45.841 4926-4926/? E/SQLiteCursor﹕ requesting column name with table name -- test@zapp.org
java.lang.Exception
at android.database.sqlite.SQLiteCursor.getColumnIndex(SQLiteCursor.java:180)
at org.reisacher.zapp.ChatActivity.updateView(ChatActivity.java:43)
at org.reisacher.zapp.MainActivity$1.onItemClick(MainActivity.java:52)
at android.widget.AdapterView.performItemClick(AdapterView.java:305)
at android.widget.AbsListView.performItemClick(AbsListView.java:1146)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3053)
at android.widget.AbsListView$3.run(AbsListView.java:3860)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
06-05 22:15:45.841 4926-4926/? E/CursorWindow﹕ Failed to read row 0, column -1 from a CursorWindow which has 5 rows, 2 columns.
06-05 22:15:45.841 4926-4926/? D/AndroidRuntime﹕ Shutting down VM
06-05 22:15:45.841 4926-4926/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: org.reisacher.zapp, PID: 4926
java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
at android.database.CursorWindow.nativeGetString(Native Method)
at android.database.CursorWindow.getString(CursorWindow.java:438)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
at org.reisacher.zapp.ChatActivity.updateView(ChatActivity.java:43)
at org.reisacher.zapp.MainActivity$1.onItemClick(MainActivity.java:52)
at android.widget.AdapterView.performItemClick(AdapterView.java:305)
at android.widget.AbsListView.performItemClick(AbsListView.java:1146)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3053)
at android.widget.AbsListView$3.run(AbsListView.java:3860)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
As one can see SQLite is trying to get the column name, which matches the one I am saving the messages to, but the somehow then reads column -1
I didn't find a solution for this behavior
Thanks
Aucun commentaire:
Enregistrer un commentaire