gpt4 book ai didi

java - Android Studio SQLite 错误 : Couldn't read row 0, 来自 CursorWindow 的第 3 列

转载 作者:行者123 更新时间:2023-12-02 11:21:21 24 4
gpt4 key购买 nike

这里是 Android Studio 新手。我正在使用 Databasehelper 文件将书籍添加到我的 HashMap 中。我的 Databasehelper 位于模型文件夹中。我相信我的代码看起来不错,但是下面发布了以下错误:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: edu.monash.libraryofalexandria, PID: 2828
java.lang.RuntimeException: Unable to start activity ComponentInfo{edu.monash.libraryofalexandria/edu.monash.libraryofalexandria.MainActivity}: java.lang.IllegalStateException: Couldn't read row 0, col 3 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.IllegalStateException: Couldn't read row 0, col 3 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 edu.monash.libraryofalexandria.models.DatabaseHelper.getAllBooks(DatabaseHelper.java:56)
at edu.monash.libraryofalexandria.MainActivity.onCreate(MainActivity.java:41)
at android.app.Activity.performCreate(Activity.java:7009)
at android.app.Activity.performCreate(Activity.java:7000)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
at android.app.ActivityThread.-wrap11(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6494) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 

这是我的 DatabaseHelper 类

public class DatabaseHelper extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "PersonDB";
public static final int DATABASE_VERSION = 1;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL(Book.CREATE_STATEMENT);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + Book.TABLE_NAME);
onCreate(sqLiteDatabase);
}
public void addBook(Book book) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(Book.COLUMN_NAME, book.getName());
values.put(Book.COLUMN_AUTHOR, book.getAuthor());
values.put(Book.COLUMN_GENRE, book.getGenre());
values.put(Book.COLUMN_ISBN, book.getISBN());
values.put(Book.COLUMN_PUBLISHER, book.getPublisher());
values.put(Book.COLUMN_DATE, book.getDate());
values.put(Book.COLUMN_EDITION, book.getEdition());
values.put(Book.COLUMN_DESCRIPTION, book.getDescription());
db.insert(Book.TABLE_NAME, null, values);
db.close();
}
public HashMap<Long, Book> getAllBooks() {
HashMap<Long, Book> books = new LinkedHashMap<>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM " + Book.TABLE_NAME, null);
// Add each book to hashmap (Each row has 1 book)
while (cursor.moveToNext()) {
Book book = new Book(cursor.getLong(0),
cursor.getString(1),
cursor.getString(2),
cursor.getString(3),
cursor.getString(4),
cursor.getString(5),
cursor.getString(6),
cursor.getString(7),
cursor.getString(8));
books.put(book.getId(), book);
}
cursor.close();
db.close();
if(books.size() == 0) {
Log.d("myTag", "This is my message");

// If there are no people in the db then add some default books
createDefaultBook();
books = getAllBooks();
}
return books;
}

public void removeBook(Book book)
{
SQLiteDatabase db = this.getWritableDatabase();
db.delete(Book.TABLE_NAME,
Book.COLUMN_ID + " = ?",
new String[] {String.valueOf(book.getId())});
}

private void createDefaultBook()
{
addBook(new Book(0, "The Green House", "William", "Romance", "26756898", "Harper", "29/2/1993", "1", "There are no men like me there is only me."));

}
}

为什么会出现这个错误?我该如何修复它?
谢谢您,
亲切的问候

最佳答案

我找到了问题的解决方案!
最初我有一个旧版本的程序,仅支持 2 列我的新版本还有 6 列,当我运行新版本时,我收到了问题中描述的错误。我通过卸载应用程序并再次运行有效的代码来修复它。

关于java - Android Studio SQLite 错误 : Couldn't read row 0, 来自 CursorWindow 的第 3 列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49895793/

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