简体   繁体   English

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

[英]Android sqlLite error - failed to read row 0 column 1 from a cursorwindow?

I am relatively new to Android Development and using SQL lite for development. 我是Android开发的新手,并且使用SQL lite进行开发。 I'm currently creating an Android application for my Final year project in university and have hit an issue when trying to run my application with a database - failed to read row 0 column 1 from a cursorwindow. 我目前正在为大学的大学预科项目创建一个Android应用程序,并且在尝试使用数据库运行我的应用程序时遇到问题-无法从游标窗口读取第0行第1列。 I have had a look through some of the questions and answers already posted and have tried some of the answers, but can't get it to work. 我浏览了一些已经发布的问题和答案,并尝试了一些答案,但无法正常工作。

Here is my code: 这是我的代码:

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();
} }

and here is the error message that i am getting: 这是我收到的错误消息:

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.

Any ideas what is going wrong? 任何想法出了什么问题?

Thanks! 谢谢!

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

It means that you have 4 columns with numbers 0, 1, 2, 3 and you try to get number 5 that doesn't exist. 这意味着您有4个数字为0、1、2、3的并且尝试获取不存在的数字5

And now, look to your creation code. 现在,查看您的创建代码。 You forgot commas after "TEXT" 您忘记了“ TEXT”之后的逗号

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);

PS don't forget to uninstall application after changing onCreate code, to install new version PS不要忘记更改onCreate代码后卸载应用程序,以安装新版本

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法从CursorWindow(Android)读取第0行第1列 - Failed to read row 0, column 1 from a CursorWindow (Android) 无法从CursorWindow读取第0行,第-1列 - Failed to read row 0, column -1 from a CursorWindow 错误:无法从具有1行和1列的cursorWindow读取第0行第1列 - error : failed to read row 0 column 1 from cursorWindow which has 1 row and 1 column 在android的sqlite中插入数据时出现错误无法从具有19行,2列的CursorWindow中读取第0行,第-1列 - getting error when insert data in sqlite in android Failed to read row 0, column -1 from a CursorWindow which has 19 rows, 2 columns 无法从CursorWindow中读取第0行第4列,该行有1行和4列 - Failed to read row 0, column 4 from CursorWindow, which has 1 row and 4 columns 无法从具有2行1列的CursorWindow中读取第1行,第1列 - Failed to read row 1, column 1 from CursorWindow which has 2 rows, 1 columns 无法从具有2行2列的CursorWindow中读取第1行第-1列 - Failed to read row 1, column -1 from a CursorWindow which has 2 rows, 2 columns 无法从具有3行1列的CursorWindow中读取第1行第-1列 - Failed to read row 1, column -1 from a CursorWindow which has 3 rows, 1 columns 无法从具有3行,3列的CursorWindow中读取第0行,第3列 - Failed to read row 0, column 3 from a CursorWindow which has 3 rows, 3 columns 无法从具有1行1列的CursorWindow中读取第0行,第1列 - Failed to read row 0, column 1 from a CursorWindow which has 1 rows, 1 columns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM