2

我有一个 TOTALCOUNT 表,其中包含一个 PK _id 列和一个整数列“CARDNUM”。我想将 CARDNUM 的默认值设置为零。然后每次将新行插入数据库时​​ CARDNUM 增加 +1。所以 5 行插入将为 rowids 1 到 5 产生 1,2,3,4,5。当我将数据推送到 RecyclerView 中的各个 CardViews 时,我只得到默认值零。前五个 CardView 应显示 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;
}    
4

1 回答 1

0

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

或者通过你的方式,但是你在处理Cursor时错过了 尝试这种方式


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

于 2016-05-13T00:39:01.937 回答