Android sqlLite 错误 - 无法从光标窗口读取第 0 行第 1 列



我对Android开发相对较新,并使用SQL lite进行开发。我目前正在为大学最后一年的项目创建一个 Android 应用程序,并且在尝试使用数据库运行我的应用程序时遇到了一个问题 - 无法从光标窗口读取第 0 行第 1 列。我已经浏览了一些已经发布的问题和答案,并尝试了一些答案,但无法使其正常工作。

这是我的代码:

public class DatabaseHandler extends SQLiteOpenHelper {
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "LtssDyslexiaDb";
// Contacts table name
private static final String TABLE_STUDENTS = "student";
// Contacts Table Columns names
private static final String ST_ID = "id";
private static final String ST_FIRST_NAME = "firstName";
private static final String ST_SURNAME = "surname";
private static final String ST_SCHOOL = "school";
private static final String ST_READING_LEVEL = "reading_Level";
private static final String ST_AGE = "age";
public DatabaseHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_STUDENTS + "("
            + ST_ID + " INTEGER PRIMARY KEY," + ST_FIRST_NAME + " TEXT,"
            + ST_SURNAME + " TEXT" + ST_SCHOOL + " TEXT,"
            + ST_READING_LEVEL + " TEXT" + ST_AGE + " TEXT" + ")";
    db.execSQL(CREATE_CONTACTS_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_STUDENTS);
    // Create tables again
    onCreate(db);
}
/**
 * All CRUD(Create, Read, Update, Delete) Operations
 */
// Adding new contact
void addStudentProfile(StudentProfile sProfile) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(ST_FIRST_NAME, sProfile.getFirstName()); // Contact Name
    //values.put(KEY_PH_NO, sProfile.getPhoneNumber()); // Contact Phone
    // Inserting Row
    db.insert(TABLE_STUDENTS, null, values);
    db.close(); // Closing database connection
}
// Getting single contact
StudentProfile getStudentProfile(int id) {
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(TABLE_STUDENTS, new String[] { ST_ID,
            ST_FIRST_NAME, ST_SURNAME, ST_SCHOOL, ST_READING_LEVEL, ST_AGE}, ST_ID + "=?",
            new String[] { String.valueOf(id) }, null, null, null, null);
    if (cursor != null)
        cursor.moveToFirst();
    StudentProfile sProfile = new StudentProfile(Integer.parseInt(cursor.getString(0)),
            cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5));
    // return contact
    return sProfile;
}
// Getting All Contacts
public List<StudentProfile> getAllContacts() {
    List<StudentProfile> contactList = new ArrayList<StudentProfile>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_STUDENTS;
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);
    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            StudentProfile student = new StudentProfile();
            student.setID(Integer.parseInt(cursor.getString(0)));
            student.setFirstName(cursor.getString(1));
            student.setSurname(cursor.getString(2));
            student.setSchool(cursor.getString(3));
            student.setReadingLevel(cursor.getString(4));
            student.setAge(cursor.getString(5));
            String name = cursor.getString(1) +" "+ cursor.getString(2);
            MainActivity.ArrayofName.add(name);
            // Adding contact to list
            contactList.add(student);
        } while (cursor.moveToNext());
    }
    // return contact list
    return contactList;
}
// Updating single contact
public int updateContact(StudentProfile sProfile) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put((ST_FIRST_NAME + ST_SURNAME), (sProfile.getFirstName()+sProfile.getSurname()));
    //values.put(KEY_PH_NO, sProfile.getPhoneNumber());
    // updating row
    return db.update(TABLE_STUDENTS, values, ST_ID + " = ?",
            new String[] { String.valueOf(sProfile.getID()) });
}
// Deleting single contact
public void deleteContact(StudentProfile sProfile) {
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(TABLE_STUDENTS, ST_ID + " = ?",
            new String[] { String.valueOf(sProfile.getID()) });
    db.close();
}

// Getting contacts Count
public int getContactsCount() {
    String countQuery = "SELECT  * FROM " + TABLE_STUDENTS;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(countQuery, null);
    cursor.close();
    // return count
    return cursor.getCount();
} }

这是我收到的错误消息:

01-30 17:11:53.042: E/CursorWindow(32734): Failed to read row 0, column 4 from a CursorWindow which has 3 rows, 4 columns.

任何想法出了什么问题?

谢谢!

Failed to read row 0, column 4 from a CursorWindow which has 3 rows, 4 columns

这意味着您有 4 列,数字为 0、1、2、3,并且您尝试获得不存在的数字 5

现在,查看您的创建代码。您忘记了"文本"后面的逗号

String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_STUDENTS + "("
        + ST_ID + " INTEGER PRIMARY KEY,"
        + ST_FIRST_NAME + " TEXT,"
  >>>   + ST_SURNAME + " TEXT" // should to " TEXT, "
        + ST_SCHOOL + " TEXT,"
  >>>   + ST_READING_LEVEL + " TEXT" // should to " TEXT, "
        + ST_AGE + " TEXT" + ")";
db.execSQL(CREATE_CONTACTS_TABLE);

附言不要忘记在更改后卸载应用程序创建代码,以安装新版本

最新更新