gpt4 book ai didi

android - 从 1 行 4 列的 CursorWindow 读取第 0 行第 4 列失败

转载 作者:太空狗 更新时间:2023-10-29 16:03:56 24 4
gpt4 key购买 nike

我正在尝试创建一个 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 个元素)。这就是异常的解释。

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

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

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

关于android - 从 1 行 4 列的 CursorWindow 读取第 0 行第 4 列失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21823621/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com