我在更新SQLite数据库时遇到问题。我想通过更新名为" burstRead"的字段来记录读取记录时的记录。创建记录时,我将字段设置为0(表示它未读)。创建表并添加记录时的代码如下:
// burst Table Columns names
private static final String KEY_ID = "burstID";
private static final String KEY_ACCTNAME = "acctName";
private static final String KEY_DATE = "burstDate";
private static final String KEY_TITLE = "burstTitle";
private static final String KEY_DESCRIPTION = "burstDescription";
private static final String KEY_CONTACT = "burstContact";
private static final String KEY_ADDRESS = "burstAddress";
private static final String KEY_REMINDER = "burstReminder";
private static final String KEY_READ = "burstRead";
private static final String KEY_IMG = "burstImg";
private static final String KEY_FILE = "burstFile";
private static final String KEY_ID = "burstID";
private static final String KEY_ACCTNAME = "acctName";
private static final String KEY_DATE = "burstDate";
private static final String KEY_TITLE = "burstTitle";
private static final String KEY_DESCRIPTION = "burstDescription";
private static final String KEY_CONTACT = "burstContact";
private static final String KEY_ADDRESS = "burstAddress";
private static final String KEY_REMINDER = "burstReminder";
private static final String KEY_READ = "burstRead";
private static final String KEY_IMG = "burstImg";
private static final String KEY_FILE = "burstFile";
String CREATE_BURSTS_TABLE = "CREATE TABLE " + TABLE_BURSTS + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_ACCTNAME + " TEXT,"
+ KEY_DATE + " TEXT," + KEY_TITLE + " TEXT," + KEY_DESCRIPTION
+ " TEXT," + KEY_IMG + " TEXT," + KEY_CONTACT + " TEXT,"
+ KEY_ADDRESS + " TEXT," + KEY_REMINDER + " TEXT," + KEY_FILE
+ " TEXT," + KEY_READ + " INTEGER)";
db.execSQL(CREATE_BURSTS_TABLE);
// Adding new contact
public void addBurst(Burst burst) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_ID, burst.getBurstID()); // Burst ID
values.put(KEY_ACCTNAME, burst.getAcctName()); // Burst Account Name
values.put(KEY_DATE, burst.getBurstDate()); // Burst Date
values.put(KEY_TITLE, burst.getBurstTitle()); // Burst Title
values.put(KEY_DESCRIPTION, burst.getBurstDescription()); // Burst Description
values.put(KEY_CONTACT, burst.getBurstContact()); // Burst Contact
values.put(KEY_ADDRESS, burst.getBurstAddress()); // Burst Address
values.put(KEY_REMINDER, burst.getBurstReminder()); // Burst Reminder
values.put(KEY_IMG, burst.getBurstImg()); // Burst Image
values.put(KEY_FILE, burst.getBurstFile()); // Burst File
values.put(KEY_READ, 0); // Burst Read
// Inserting Row
db.insert(TABLE_BURSTS, null, values);
db.close(); // Closing database connection
}
values.put(KEY_READ,0); 我相信这是在插入记录时将字段设置为零的正确方法。
更新脚本如下:
public void updateBurstRead(int burstID) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_READ, 1);
db.update(TABLE_BURSTS, values, KEY_ID + " = ?", new String[] { String.valueOf(burstID) });
db.close(); // Closing database connection
}
应用程序的工作原理是MainActivity.java包含一个列表视图,当点击它时会启动名为DetailActivity.java的意图,捆绑burstID以便可以在新活动中查看Burst的内容。此时函数updateBurstRead()应该执行并将burstRead更新为1.
这就是我在DetailActivity.java中调用和更新必要的函数和变量的方法
Burst data = new Burst();
private int burstID;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
// Show the Up button in the action bar.
setupActionBar();
DatabaseHandler db = new DatabaseHandler(this);
Bundle b = getIntent().getExtras();
burstID = b.getInt("burstID");
data = db.getBurst(burstID);
db.updateBurstRead(burstID);
...
我知道正在调用并执行updateBurstRead函数而没有任何错误,但记录没有被更新。
那可能是什么问题?