Ett*_*tty 2 database sqlite android
我正在尝试创建一个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;
}
}
Run Code Online (Sandbox Code Playgroud)
列索引从0开始.您从仅有4列的游标请求索引4(第5个元素).这是例外的解释.
为什么SELECT *
来自5列表只返回4列:您最近添加了另一列,您需要确保数据库文件反映此更改.一些选择:
删除旧的数据库文件:例如卸载应用程序.这可确保onCreate()
再次运行数据库帮助程序以从头创建新数据库.
增加1
传递给SQLiteOpenHelper
super
构造函数的数据库版本.由于磁盘上的文件是版本1,因此请求更大的版本将onUpgrade()
调用您的回调,您可以使用新列更新数据库文件.
归档时间: |
|
查看次数: |
8319 次 |
最近记录: |