无法从CursorWindow读取第0行第4列,其中包含1行和4列

时间:2014-02-17 07:58:43

标签: android database sqlite

我正在尝试创建一个SQLite数据库,所有看起来都没问题,直到我在列表中添加“profileWeight”。我试图查看其他样本,但都与我的不同。任何人都可以帮助我。我的DBController.java的代码如下:

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

2 个答案:

答案 0 :(得分:6)

列索引从0开始。您从仅有4列的游标请求索引4(第5个元素)。这就是例外的解释。

为什么来自5列表的SELECT *仅返回4列:您最近添加了另一列,您需要确保数据库文件反映此更改。一些选择:

  • 删除旧数据库文件:例如卸载应用程序。这可确保再次运行数据库帮助程序onCreate()以从头开始创建新数据库。

  • 增加传递给1 SQLiteOpenHelper构造函数的数据库版本super。由于磁盘上的文件是版本1,因此请求更大的版本将调用onUpgrade()回调,您可以使用新列更新数据库文件。

答案 1 :(得分:1)

我怀疑profileWeight不在数据库中 您需要增加数据库的版本,以便调用onUpgradeonCreate方法并使用profileWeight字段重新创建数据库。

版本在此行中指定

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

尝试将1更改为2。