[英]SQLite database table updating all but one field
因此,我正在開發一個Android應用程序,其中將數據保存在Android的SQLite數據庫中。 由於某些原因,streakCategory和daysKept可以很好地更新,但streakName不會更新。 有人知道為什么嗎? 我的代碼與streakCategory和daysKept相同。
EditStreak.java的代碼段 :
doneButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
editor.putString("currButtonActivityName", streakIcon.getText().toString()).commit();
editor.putString("currButtonActivityCategory", categoryIcon.getText().toString()).commit();
editor.putInt("currButtonDaysKept", Integer.parseInt(streakDaysKept.getText().toString().trim())).commit();
String updateName = prefs.getString("currButtonActivityName", "").trim();
String updateCategory = prefs.getString("currButtonActivityCategory", "").trim();
int updateDaysKept = prefs.getInt("currButtonDaysKept", 0);
boolean isUpdated = db.updateData(updateName, updateCategory, updateDaysKept);
Log.d("Name: ", prefs.getString("currButtonActivityName", ""));
if (isUpdated == true){
Log.d("carter.streakly", "AFTER SUCCESS: ID: " + prefs.getInt("currButtonID", 0) + " Name: " + prefs.getString("currButtonActivityName", "") + " Category: " +
prefs.getString("currButtonActivityCategory", "") + " Days Kept: " + prefs.getInt("currButtonDaysKept", 9));
Intent intent = new Intent(EditStreak.this, EnlargedActivity.class);
startActivity(intent);
finish();
}
else{
Toast.makeText(EditStreak.this, "Data not Updated", Toast.LENGTH_LONG);
}
}
});
DatabaseHelper.java
public class DatabaseHelper extends SQLiteOpenHelper{
public static final String DATABASE_NAME = "streaks.db"; // Name of DB
public static final String TABLE_NAME = "streak_table";
public static final String COL_1 = "ID";
public static final String COL_2 = "STREAKNAME";
public static final String COL_3 = "STREAKCATEGORY";
public static final String COL_4 = "DATESTARTED";
public static final String COL_5 = "DAYSKEPT";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT,STREAKNAME TEXT,STREAKCATEGORY TEXT,DATESTARTED TEXT,DAYSKEPT INTEGER);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
onCreate(db);
}
public boolean insertData(String STREAKNAME, String STREAKCATEGORY, String DATESTARTED, int DAYSKEPT){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2, STREAKNAME);
contentValues.put(COL_3, STREAKCATEGORY);
contentValues.put(COL_4, DATESTARTED);
contentValues.put(COL_5, DAYSKEPT);
long result = db.insert(TABLE_NAME, null, contentValues);
if(result == -1){
return false;
} else {
db.close();
return true;
}
}
public Cursor getAllData(){
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("SELECT * FROM "+TABLE_NAME,null);
return res;
}
public boolean updateData(String streakName, String streakCategory, int daysKept){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2, streakName);
contentValues.put(COL_3, streakCategory);
contentValues.put(COL_5, daysKept);
db.update(TABLE_NAME, contentValues, "STREAKNAME = ?", new String[] {streakName});
return true;
}
public Integer deleteData(String streakName){
SQLiteDatabase db = this.getWritableDatabase();
return db.delete(TABLE_NAME, "STREAKNAME = ?", new String[] {streakName});
}
public boolean vacuum(){
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("VACUUM");
return true;
}
}
您可以嘗試這樣的事情:
SQLiteDatabase db = this.getWritableDatabase();
db.beginTransaction();
SQLiteStatement upd=db.compileStatement("UPDATE "+TABLE_NAME+" SET "+COLUMN_NAME+"=VALUE WHERE "+STREAKNAME +"=?");
upd.bindString(1, streakNameValue);
upd.execute();
db.setTransactionSuccessful();
db.endTransaction();
Log.e("update", "done");
會根據邏輯改變嗎? 您正在基於streakName查詢記錄並更新相同的名稱。
......
contentValues.put(COL_2, streakName); // streakName = "abcd"
......
db.update(TABLE_NAME, contentValues, "STREAKNAME = ?", new String[] {streakName});
return true;
// here you are querying records which have streakName as "abcd" already, so it wont change
否則,您需要更改它以通過記錄的ID查詢它,或者傳遞舊的Streakname(必須用新的StreakName替換)。
db.update(TABLE_NAME, contentValues, "STREAKID = ?", new String[] {streakID});
return true;
要么
contentValues.put(COL_2, NEWstreakName);
db.update(TABLE_NAME, contentValues, "STREAKNAME = ?", new String[] {OLDStreakName});
return true;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.