SQLite数据库表更新除一个字段外的所有字段

时间:2016-08-10 04:43:07

标签: java android sqlite

所以我正在开发一个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;
}
}

2 个答案:

答案 0 :(得分: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");

答案 1 :(得分:0)

这会根据逻辑改变吗? 您正在查询基于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;