今天我想在我的消息应用程序中实现一个简单的SQLite数据库来处理保存消息,遗憾的是我无法从数据库中读回数据并将它们显示到ListView中。
我在模拟器上通过ADB查看数据库,我看到数据已正确保存。
这是我的代码
使用空列初始化TABLE
db.execSQL("CREATE TABLE IF NOT EXISTS Messages(zero);");
使用聊天联系人作为列名创建列
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 + "');");
}
在这里我还可以看到SQLite识别我的列名,因为当我向客户端发送多条消息时,我收到调试消息
E/SQLiteLog﹕ (1) duplicate column name: test@zapp.org
这是我的读数无效
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();
}
这是我的例外
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)
正如人们可以看到SQLite正在尝试获取列名,该列名与我保存消息的列匹配,但不知何故,然后读取列-1
我找不到这种行为的解决方案
由于
答案 0 :(得分:0)
问题是你有一个'。'在字段名称中,引擎将其理解为table.field分隔符。
换句话说,getColumnIndex()正试图找到' org'的数字索引。在表格中测试@ zapp' ...不在字段名称中使用该类型值的另一个原因。
提供它的线索是" E / SQLiteCursor:请求带有表名的列名 - test@zapp.org"
我还会质疑您根据'从'动态添加列到表格的想法。值,水平增长你的表而不是让它垂直扩展的常规方法。随着它越来越大,越来越笨重,它变得越来越慢。