Sqlite:如何将行值增加+1?

时间:2016-05-13 00:16:55

标签: sqlite android-cursor content-values

我有一个TOTALCOUNT表,其中包含PK _id列和一个整数列“CARDNUM”。我想将CARDNUM的默认值设置为零。每次将新行插入数据库时​​,将CARDNUM增加+1。所以5行插入将产生1,2,3,4,5用于rowid 1到5.当我将数据推送到我的RecyclerView中的各个CardView时,我只获得默认值零。前五个CardViews应显示CARDNUM值1,2,3,4,5。我在这里缺少什么?

DBContract文件:

...
public static final String TABLE_NAME_TOTALCOUNT = "totalcount";
    public static final String COLUMN_NAME_COUNTID = "_id";
    public static final String COLUMN_NAME_CARDNUM = "cardnum";
    }
}

    A second table, not shown is called USERINPUTS.

DBHelper文件:

...
public void onCreate(SQLiteDatabase db) {
    // Set up the Column headings for USERINPUTS and TOTALCOUNT Tables.
    db.execSQL(SQL_CREATE_ENTRIES);
    db.execSQL(SQL_CREATE_CARDNUM);

private static final String SQL_CREATE_CARDNUM =
    "CREATE TABLE IF NOT EXISTS "+ DBContract.DBEntry.TABLE_NAME_TOTALCOUNT +
            "( "+ DBContract.DBEntry.COLUMN_NAME_COUNTID +
            " INTEGER PRIMARY KEY , "+
            DBContract.DBEntry.COLUMN_NAME_CARDNUM +
            " INTEGER);";

public void insertIntoTableTOTALCOUNT() {

    SQLiteDatabase db = this.getWritableDatabase();

    db.beginTransaction();

    String select = "Select * from "+ DBContract.DBEntry.TABLE_NAME_TOTALCOUNT;

    Cursor c =db.rawQuery(select, null);
    int cardnum=0;

    try {
        if (c.moveToFirst()) {
            cardnum = c.getInt(1);
            cardnum++;
        }
    } finally {
        if(c !=null && !c.isClosed()){
            c.close();
        }
    }

    ContentValues cv = new ContentValues();
    cv.put(DBContract.DBEntry.COLUMN_NAME_CARDNUM,cardnum);
    db.insert(DBContract.DBEntry.TABLE_NAME_TOTALCOUNT,null,cv);

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

...
// Two Tables push the data to a RecyclerView list.
public List<UserData> getDataFromDB(){
    List<UserData> modelList = new ArrayList<>();

    String query = "SELECT *, (SELECT " + DBContract.DBEntry.COLUMN_NAME_CARDNUM +
            " from " + DBContract.DBEntry.TABLE_NAME_TOTALCOUNT +") from "
            + DBContract.DBEntry.TABLE_NAME_USERINPUTS +";";

    SQLiteDatabase db = this.getReadableDatabase();

    db.beginTransaction();

    Cursor cursor = db.rawQuery(query, null);

    Log.d("TAG", DatabaseUtils.dumpCursorToString(cursor));

        try {
            if (cursor.moveToFirst()) {
                do {
                    UserData userData = new UserData();
                    userData.setTodo(cursor.getString(1));
                    userData.setNote1(cursor.getString(2));
                    userData.setNote2(cursor.getString(3));
                    userData.setDuedate(cursor.getString(4));
                    userData.setDuetime(cursor.getString(5));
                    userData.setTimestamp(cursor.getLong(6));
                    userData.setCardnum(cursor.getInt(7));

                    modelList.add(0, userData);
                    } while (cursor.moveToNext());
            }
        } finally {
              if(cursor !=null && !cursor.isClosed()){
              cursor.close();
              }
          }
    db.setTransactionSuccessful();
    db.endTransaction();
    return modelList;
}    

1 个答案:

答案 0 :(得分:0)

您可以在创建表格时轻松添加“ AUTOINCREMENT ”选项,它会自动递增

或按您的方式,但在处理光标时您缺少 试试这种方式

Cursor c =db.rawQuery(select, null);
    int cardnum=0;
c.moveToFirst();
while( c != null )
{
 cardnum = c.getInt(1);
            cardnum++;
c.moveToNext();
}