2016-08-10 40 views
2

因此,我正在開發一個Android應用程序,其中我將數據保存在Android的SQLite數據庫中。出於某種原因,streakCategory和daysKept將更新正常,但streakName不會更新。有人知道爲什麼嗎?我的代碼與streakCategory和daysKept的代碼相同。SQLite數據庫表更新除了一個字段以外的所有信息

片段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; 
} 
} 
+0

有你陪rawQuery –

+0

測試我沒有,我將如何去這樣做?我對SQLite很陌生 –

+0

你的問題解決了嗎? – Ramesh

回答

0

你可以嘗試這樣的事情:

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"); 
0

將是有史以來根據改變以logi C ?? 您正在查詢基於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   

Eithe你需要改變它通過記錄的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; 
相關問題