gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 07:37:35 25 4
gpt4 key购买 nike

所以我正在开发一个 Android 应用程序,其中的数据保存在 Android 的 SQLite 数据库中。由于某种原因, streakCategory 和 daysKept 会正常更新,但 streakName 不会更新。有人知道为什么吗?我的代码与 streakCategory 和 daysKept 的代码相同。

EditStreak.java 的 fragment :

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

关于java - SQLite 数据库表更新除一个字段之外的所有字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38864421/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com