【发布时间】:2014-02-17 16:18:45
【问题描述】:
我正在尝试创建一个 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;
}
}
【问题讨论】: