【发布时间】:2018-04-19 11:34:26
【问题描述】:
我有一个 TOTALCOUNT 表,其中包含一个名为 COUNTID 的 PK _id 列和一个名为 CARDNUM 的整数列。我在 CARDNUM 上进行了插入,因此 PK 将自动递增。但是当我在 Android Studio 中执行 LogCat 输出时,它会显示 _id = 1 用于第一次插入,然后 _id =1 用于第二次插入。我认为第二个插入应该显示_id = 2。
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 NOT NULL AUTOINCREMENT, " +
DBContract.DBEntry.COLUMN_NAME_CARDNUM +
" INTEGER" + ")";
public void insertIntoTableTOTALCOUNT() {
int cardnum = 0;
// Get a reference to a writable DB
SQLiteDatabase db = this.getWritableDatabase();
db.beginTransaction();
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();
}
Log.d("TAG", DatabaseUtils.dumpCursorToString(cursor));
【问题讨论】:
标签: android sqlite android-cursor