Etty
Etty

Reputation: 45

Failed to read row 0, column 4 from CursorWindow, which has 1 row and 4 columns

I'm trying to make an SQLite database and all seems okay until I add "profileWeight" in the list. I have tried to look into other samples but all are different from mine. Can anybody help me with this. The code for my DBController.java is as below:

public class DBController extends SQLiteOpenHelper {
private static final String LOGCAT = null;

public DBController(Context applicationcontext) {
    super(applicationcontext, "profiledatabase.db", null, 1);
    Log.d(LOGCAT, "Created");
}

@Override
public void onCreate(SQLiteDatabase database) {
    String query = "CREATE TABLE profiles ( profileId INTEGER PRIMARY KEY, profileName TEXT, "
            + "profileDOB DATE, profileSex TEXT, profileWeight TEXT)";
    database.execSQL(query);
    Log.d(LOGCAT, "profiles Created");
}

@Override
public void onUpgrade(SQLiteDatabase database, int version_old,
        int current_version) {
    String query;
    query = "DROP TABLE IF EXISTS profiles";
    database.execSQL(query);
    onCreate(database);
}

public void insertProfile(HashMap<String, String> queryValues) {
    SQLiteDatabase database = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put("profileName", queryValues.get("profileName"));
    values.put("profileDOB", queryValues.get("profileDOB"));
    values.put("profileSex", queryValues.get("profileSex"));
    values.put("profileWeight", queryValues.get("profileWeight"));

    database.insert("profiles", null, values);
    database.close();
}

public int updateProfile(HashMap<String, String> queryValues) {
    SQLiteDatabase database = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put("profileName", queryValues.get("profileName"));
    values.put("profileDOB", queryValues.get("profileDOB"));
    values.put("profileSex", queryValues.get("profileSex"));    
    values.put("profileWeight", queryValues.get("profileWeight"));      

    return database.update("profiles", values, "profileId" + " = ?",
            new String[] { queryValues.get("profileId")});
    // String updateQuery =
    // "Update  words set txtWord='"+word+"' where txtWord='"+ oldWord +"'";
    // Log.d(LOGCAT,updateQuery);
    // database.rawQuery(updateQuery, null);
    // return database.update("words", values, "txtWord  = ?", new String[]
    // { word });
}

public void deleteProfile(String id) {
    Log.d(LOGCAT, "delete");
    SQLiteDatabase database = this.getWritableDatabase();
    String deleteQuery = "DELETE FROM profiles where profileId='" + id
            + "'";
    Log.d("query", deleteQuery);
    database.execSQL(deleteQuery);
}

public ArrayList<HashMap<String, String>> getAllProfiles() {
    ArrayList<HashMap<String, String>> wordList = new ArrayList<HashMap<String, String>>();
    String selectQuery = "SELECT  * FROM profiles";
    SQLiteDatabase database = this.getWritableDatabase();
    Cursor cursor = database.rawQuery(selectQuery, null);
    if (cursor.moveToFirst()) {
        do {

            HashMap<String, String> profileMap = new HashMap<String, String>();
            profileMap.put("profileId", cursor.getString(0));
            profileMap.put("profileName", cursor.getString(1));
            profileMap.put("profileDOB", cursor.getString(2));
            profileMap.put("profileSex", cursor.getString(3));
            profileMap.put("profileWeight", cursor.getString(4));

            wordList.add(profileMap);

        } while (cursor.moveToNext());
    }

    // return contact list
    return wordList;
}

public HashMap<String, String> getProfileInfo(String id) {
    HashMap<String, String> wordList = new HashMap<String, String>();
    SQLiteDatabase database = this.getReadableDatabase();
    String selectQuery = "SELECT * FROM profiles where profileId='" + id
            + "'";
    Cursor cursor = database.rawQuery(selectQuery, null);
    if (cursor.moveToFirst()) {
        do {
            // HashMap<String, String> map = new HashMap<String, String>();
            wordList.put("profileId", cursor.getString(0));             
            wordList.put("profileName", cursor.getString(1));
            wordList.put("profileDOB", cursor.getString(2));
            wordList.put("profileSex", cursor.getString(3));    
            wordList.put("profileWeight", cursor.getString(4));                 

            // wordList.add(map);
        } while (cursor.moveToNext());
    }
    return wordList;
}
}

Upvotes: 3

Views: 11097

Answers (2)

laalto
laalto

Reputation: 152807

Column indexes start from 0. You're requesting index 4 (5th element) from a cursor that only has 4 columns. That's the explanation for the exception.

Why SELECT * from a 5-column table returns only 4 columns: You recently added another column, you'll need to ensure the database file is reflected with this change. Some options:

  • Remove the old database file: e.g. uninstall the app. This ensures that your database helper onCreate() is run again to create a new database from scratch.

  • Increase the database version 1 you pass to the SQLiteOpenHelper super constructor. Since the file on disk is version 1, requesting a larger version will make your onUpgrade() callback to be invoked where you can update the database file with the new column.

Upvotes: 7

Merlevede
Merlevede

Reputation: 8170

My suspicion is that the profileWeight is not in the database.
You need to increase the version of your database, so that the onUpgrade and onCreate method gets called and recreates your database with the profileWeight field.

The version is specified in this line

super(applicationcontext, "profiledatabase.db", null, 1);

try changing 1 for 2.

Upvotes: 2

Related Questions