我正在尝试创建一个 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;
}
}

最佳答案

列索引从 0 开始。您正在从只有 4 列的游标中请求索引 4(第 5 个元素)。这就是对异常的解释。

为什么 5 列表中的 SELECT * 仅返回 4 列:您最近添加了另一列,您需要确保此更改反射(reflect)了数据库文件。一些选项:

  • 删除旧的数据库文件:例如卸载应用程序。这可确保您的数据库助手 onCreate() 再次运行以从头开始创建新数据库。
  • 增加您传递给 1 SQLiteOpenHelper 构造函数的数据库版本 super。由于磁盘上的文件是版本 1,请求更大的版本将使您的 onUpgrade() 回调被调用,您可以在其中使用新列更新数据库文件。
  • 关于android - 无法从 CursorWindow 读取第 0 行第 4 列,该窗口有 1 行和 4 列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21823621/

    10-10 09:40