SQLite:如何在onClick之后更新表中的主键列?

时间:2016-05-11 02:43:40

标签: android sqlite primary-key

我可以将主键列用作增量计数器吗?默认值应为1.每次用户单击“保存”按钮时,我都希望表格列更新+1。然后,我将单个行值推送到RecyclerView中的CardView。如果是这样,我应该使用INSERT或UPDATE或ContentValues来添加另一行吗?

或者我应该为递增值设置第二列并将该数据推送到CardViews并单独保留主键列?

活动文件:

...
public void onClickSave(View v) {
...
int cardnum = 0;
helper.insertIntoTableTOTALCOUNT(cardnum); 

DBContract文件:

...
public static abstract class DBEntry implements BaseColumns {

    // Strings for TOTALCOUNT TABLE
    public static final String TABLE_NAME_TOTALCOUNT = "totalcount";
    public static final String COLUMN_NAME_COUNTID = "_id";
}

DBHelper文件:

...
public void insertIntoTableTOTALCOUNT(int cardnum) {

    // Get a reference to a writable DB
    SQLiteDatabase db = this.getWritableDatabase();

    db.beginTransaction();

    // Insert values into the Table
    ? 

    db.setTransactionSuccessful();
    db.endTransaction();
    // Close the DB
    if(db.isOpen())
    db.close();
}            

0 个答案:

没有答案