gpt4 book ai didi

Android SQLite 获取 CursorIndexOutOfBoundsException

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

我在使用 Cursors 对 Android 数据库执行 SQLite 查询时遇到问题,如果我尝试使用 getPage,我会得到一个 android.database.CursorIndexOutOfBoundsException: Index 0 requested, with size of 0 错误,我的游标有问题吗?做一个 cursor.getColumnCount() 得到 4 是正确的,并且有一行,所以这不是问题,什么可能导致错误?提前致谢

public class DBHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 3;

// Database Name
private static final String DATABASE_NAME = "pagesManager";

// Pages table name
private static final String TABLE_PAGES = "pages";
// Pages Table Columns names
private static final String PAGES_KEY_ID = "id";
private static final String PAGES_KEY_NAME = "pageName";
private static final String PAGES_KEY_DATE = "lastDate";
private static final String PAGES_KEY_PAGE = "pageUrl";
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_PAGES_TABLE = "CREATE TABLE " + TABLE_PAGES + "("
+ PAGES_KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + PAGES_KEY_NAME + " TEXT,"+ PAGES_KEY_DATE +
" TEXT," + PAGES_KEY_PAGE + " TEXT" +")";
db.execSQL(CREATE_PAGES_TABLE);
}

public PageListItem getPage(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_PAGES, new String[] { PAGES_KEY_ID, PAGES_KEY_NAME, PAGES_KEY_DATE, PAGES_KEY_PAGE }, PAGES_KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
if(cursor.moveToFirst()) {
System.out.println("Cursor no es nulo");
}
PageListItem page = new PageListItem(cursor.getString(1),cursor.getString(2), cursor.getString(3),Integer.parseInt(cursor.getString(0)));
cursor.close();
return page;
}
}

最佳答案

我使用了错误的 ID,因此 getPage() 无法找到它。

关于Android SQLite 获取 CursorIndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10792400/

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