gpt4 book ai didi

android - 使用游标获取列内容

转载 作者:行者123 更新时间:2023-11-30 03:23:17 25 4
gpt4 key购买 nike

我有一个从 SQLiteDataBase 对象返回的游标,我正在使用游标 getXXX() 方法从游标中获取列的内容,但出现错误。我的代码是从数据库中获取游标并获取对象...

public List<Message> getAllMessages(char ch) {
Cursor cursor = null;
List<Message> message = new ArrayList<Message>();
if(ch=='r')
{
String queryz = "SELECT " + MySQLiteHelper.COLUMN_ID + "," + MySQLiteHelper.COLUMN_MESSAGE + " FROM " + MySQLiteHelper.TABLE_NAME + " WHERE " +MySQLiteHelper.COLUMN_STATUS + "= 'r'";
cursor = database.rawQuery(queryz, null);
}
if(ch=='s')
{
String queryz = "SELECT " + MySQLiteHelper.COLUMN_ID + "," + MySQLiteHelper.COLUMN_MESSAGE + " FROM " + MySQLiteHelper.TABLE_NAME + " WHERE " +MySQLiteHelper.COLUMN_STATUS + "= 's'";
cursor = database.rawQuery(queryz, null);
}


cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Message message1 = cursorToMessage(cursor);
message.add(message1);
cursor.moveToNext();
}
// Make sure to close the cursor
cursor.close();
return message;
}

public Message createMessage(String message,char ch,int random) {
ContentValues values = new ContentValues();

if(ch=='s')
values.put(MySQLiteHelper.COLUMN_STATUS, "s");
if(ch=='r')
values.put(MySQLiteHelper.COLUMN_STATUS, "r");

values.put(MySQLiteHelper.COLUMN_PARENTID, 0);

values.put(MySQLiteHelper.COLUMN_THREADID, random);

values.put(MySQLiteHelper.COLUMN_MESSAGE, message);
long insertId = database.insert(MySQLiteHelper.TABLE_NAME, null,
values);


Cursor cursor = database.query(MySQLiteHelper.TABLE_NAME,
allColumns, MySQLiteHelper.COLUMN_ID + " = " + insertId, null,
null, null, null);
/* Cursor cursor = database.query(MySQLiteHelper.TABLE_NAME,
allColumns, null, null,
null, null, null);*/
cursor.moveToFirst();
Message newMessage = cursorToMessage(cursor);
cursor.close();
return newMessage;
}
private Message cursorToMessage(Cursor cursor) {
Message message = new Message();
message.setId(cursor.getLong(cursor.getColumnIndex("id")));

message.setmessage(cursor.getString(cursor.getColumnIndex("message")));
message.setthreadid(cursor.getLong(cursor.getColumnIndex("threadid")));

return message;
}

My log-cat is:

09-11 17:15:01.227: E/AndroidRuntime(4707): FATAL EXCEPTION: main
09-11 17:15:01.227: E/AndroidRuntime(4707): java.lang.RuntimeException: Unable to start activity ComponentInfo{soft.b.peopleassist/soft.b.peopleassist.MainActivity}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
09-11 17:15:01.227: E/AndroidRuntime(4707): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
09-11 17:15:01.227: E/AndroidRuntime(4707): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
09-11 17:15:01.227: E/AndroidRuntime(4707): at android.app.ActivityThread.access$600(ActivityThread.java:122)
09-11 17:15:01.227: E/AndroidRuntime(4707): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
09-11 17:15:01.227: E/AndroidRuntime(4707): at android.os.Handler.dispatchMessage(Handler.java:99)
09-11 17:15:01.227: E/AndroidRuntime(4707): at android.os.Looper.loop(Looper.java:137)
09-11 17:15:01.227: E/AndroidRuntime(4707): at android.app.ActivityThread.main(ActivityThread.java:4340)
09-11 17:15:01.227: E/AndroidRuntime(4707): at java.lang.reflect.Method.invokeNative(Native Method)
09-11 17:15:01.227: E/AndroidRuntime(4707): at java.lang.reflect.Method.invoke(Method.java:511)
09-11 17:15:01.227: E/AndroidRuntime(4707): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-11 17:15:01.227: E/AndroidRuntime(4707): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-11 17:15:01.227: E/AndroidRuntime(4707): at dalvik.system.NativeStart.main(Native Method)
09-11 17:15:01.227: E/AndroidRuntime(4707): Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
09-11 17:15:01.227: E/AndroidRuntime(4707): at android.database.CursorWindow.nativeGetLong(Native Method)
09-11 17:15:01.227: E/AndroidRuntime(4707): at android.database.CursorWindow.getLong(CursorWindow.java:526)
09-11 17:15:01.227: E/AndroidRuntime(4707): at android.database.AbstractWindowedCursor.getLong(AbstractWindowedCursor.java:75)
09-11 17:15:01.227: E/AndroidRuntime(4707): at soft.b.peopleassist.DataSource.cursorToMessage(DataSource.java:104)
09-11 17:15:01.227: E/AndroidRuntime(4707): at soft.b.peopleassist.DataSource.getAllMessages(DataSource.java:89)
09-11 17:15:01.227: E/AndroidRuntime(4707): at soft.b.peopleassist.Send.onCreateView(Send.java:58)
09-11 17:15:01.227: E/AndroidRuntime(4707): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:795)
09-11 17:15:01.227: E/AndroidRuntime(4707): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:998)
09-11 17:15:01.227: E/AndroidRuntime(4707): at android.app.BackStackRecord.run(BackStackRecord.java:622)
09-11 17:15:01.227: E/AndroidRuntime(4707): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1330)
09-11 17:15:01.227: E/AndroidRuntime(4707): at android.app.Activity.performStart(Activity.java:4474)
09-11 17:15:01.227: E/AndroidRuntime(4707): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1928)

最佳答案

需要进行两项修改。添加

cursor.moveToFirst();

cursor.close();

进入 cursorToMessage()


我做了一些修改。使用下面的代码

// cursor.moveToFirst(); // No need to move position
Message newMessage = cursorToMessage(cursor);
// cursor.close(); // No need to close here

private Message cursorToMessage(Cursor cursor) {
if (cursor != null && cursor.moveToFirst()) {
Message message = new Message();
message.setId(cursor.getLong(cursor.getColumnIndex("id")));
message.setmessage(cursor.getString(cursor.getColumnIndex("message")));
message.setthreadid(cursor.getLong(cursor.getColumnIndex("threadid")));
cursor.close();
return message;
}
return null;
}

确保您正在将数据库中的所有列 id, message, threadid 读取到 Cursor 中。为确保您可以看到 allColumns 的值。

关于android - 使用游标获取列内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18733332/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com