我有一个TOTALCOUNT表,其中包含PK _id列和一个整数列“ CARDNUM”。我想将CARDNUM的默认值设置为零。每次将新行插入数据库时​​,将CARDNUM加+1。因此,将5行插入将为行号1到5生成1,2,3,4,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;
}

最佳答案

您可以通过在创建表格时添加“ AUTOINCREMENT”选项来轻松实现,它将自动递增

或顺便说一句,但是在与游标打交道时却不见了
尝试这种方式



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

09-28 12:11