简体   繁体   English

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

[英]SQLite database table updating all but one field

So I'm working on an Android app where I have data saved in Android's SQLite database. 因此,我正在开发一个Android应用程序,其中将数据保存在Android的SQLite数据库中。 For some reason, streakCategory and daysKept will update fine, but streakName will not update. 由于某些原因,streakCategory和daysKept可以很好地更新,但streakName不会更新。 Does anybody have any idea why? 有人知道为什么吗? My code is the same as it is for streakCategory and daysKept. 我的代码与streakCategory和daysKept相同。

Snippet of EditStreak.java : 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 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;
}
}

You can try something like this : 您可以尝试这样的事情:

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

Would that ever change according to logic ?? 会根据逻辑改变吗? you are querying the record based on streakName and updating the same name. 您正在基于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 you need to change it to query it by id of record or pass old streakname which has to be replaced by this new streakName. 否则,您需要更改它以通过记录的ID查询它,或者传递旧的Streakname(必须用新的StreakName替换)。

db.update(TABLE_NAME, contentValues, "STREAKID = ?", new String[] {streakID});
        return true;

or 要么

contentValues.put(COL_2, NEWstreakName);

db.update(TABLE_NAME, contentValues, "STREAKNAME = ?", new String[] {OLDStreakName});
        return true;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM