android - 获取插入后的自增值

标签 android android-intent sqlite

这是我用来将一些数据插入数据库的插入方法:

public long insertBooking(String userId,int roomId,Timestamp checkInTime, Timestamp checkOutTime){
    final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    ContentValues contentValues=new ContentValues();

    contentValues.put(USER_ID, userId);
    contentValues.put(ROOM_ID, roomId);
    contentValues.put(CHECK_IN, dateFormat.format(checkInTime));
    contentValues.put(CHECK_OUT, dateFormat.format(checkOutTime));

    return database.insert(MRM_BOOKING_DETAILS_TABLE, null, contentValues);

}

MRM_BOOKING_DETAILS 表有一个名为 booking_id 的自动递增字段。插入新行后,该列的值增加 1。是否可以在上述方法本身完成插入后返回该行的 booking_id 而无需编写单独的选择查询?

或者,我是否必须编写单独的 SQL Select 查询来返回新插入行的 booking_id

最佳答案

long insertId = database.insert(MRM_BOOKING_DETAILS_TABLE, null, contentValues); 此行本身返回插入行的 booking_id...

关于android - 获取插入后的自增值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10213311/

相关文章:

android - RecyclerView ItemTouchHelper 滑动操作在 fragment 类中不起作用

java - 如何禁用 RecyclerView 和 AsyncTask 中的按钮

android - 转弯多点路线导航 Google Maps Intent on Android

sql - 没有从 SQlite 表中提取任何内容?

python - 我需要 sqlite3 的提示

android - 如何在android中暂停不同的音乐播放器?

android - React-Native 通过教程 Facebook 获取 JSON 返回错误

android - 从 Intent.createChooser() 启动时如何防止应用程序/Activity 的多个实例?

android studio 自定义适配器 OnClickListener

mysql - 数据采集​​用什么数据库?