Android 事务体中的自动增量值
如何获取thansaction主体中的自动增量值 代码Android 事务体中的自动增量值,android,sql,sqlite,transactions,Android,Sql,Sqlite,Transactions,如何获取thansaction主体中的自动增量值 代码 public void insertAllStudents(列出学生){ 字符串sql=“插入到”+studentry.TABLE_NAME+“值(?,,,?,?);”; SQLiteDatabase db=this.getWritableDatabase(); SQLiteStatement=db.compileStatement(sql); db.beginTransaction(); 用于(学生:学生){ 语句。clearBindin
public void insertAllStudents(列出学生){
字符串sql=“插入到”+studentry.TABLE_NAME+“值(?,,,?,?);”;
SQLiteDatabase db=this.getWritableDatabase();
SQLiteStatement=db.compileStatement(sql);
db.beginTransaction();
用于(学生:学生){
语句。clearBindings();
语句.bindString(2,student.getId());
语句.bindString(3,student.getFirstName());
语句.bindString(4,student.getLastName());
statement.bindLong(5,student.getbirth());
语句。execute();
}
db.setTransactionSuccessful();
db.endTransaction();
}
第一列(_ID)是自动增量字段。这是获得该值的机会吗?
student.getId()-这不是数据库中的id,而是不同的id。如果将代码更改为使用db.insert(),此方法将返回插入行的id-请参阅 如果希望继续编译语句,还可以使用专门的SQLite函数获取最后插入的行,请参阅 编辑:使用db.insert()的示例。这是没有测试,但应该非常接近功能
db.beginTransaction();
boolean success = true;
final ContentValues values = new ContentValues();
for (final Student student: students) {
values.put("student_id", student.getId());
values.put("first_name", student.getFirstName());
values.put("last_name", student.getLastName());
values.put("birthday", student.getBirthday());
final long id = db.insert("my_table", null, values);
if (id == -1) {
success = false;
break;
}
// TODO do your thing with id here.
}
if (success) {
db.setTransactionSuccessful();
}
db.endTransaction();
如果将代码更改为使用db.insert(),此方法将返回插入行的id-请参阅 如果希望继续编译语句,还可以使用专门的SQLite函数获取最后插入的行,请参阅 编辑:使用db.insert()的示例。这是没有测试,但应该非常接近功能
db.beginTransaction();
boolean success = true;
final ContentValues values = new ContentValues();
for (final Student student: students) {
values.put("student_id", student.getId());
values.put("first_name", student.getFirstName());
values.put("last_name", student.getLastName());
values.put("birthday", student.getBirthday());
final long id = db.insert("my_table", null, values);
if (id == -1) {
success = false;
break;
}
// TODO do your thing with id here.
}
if (success) {
db.setTransactionSuccessful();
}
db.endTransaction();
您可以执行以下操作,而不是
语句.execute()
。这将返回插入行的行ID。或者,正如@Tom所建议的,您可以使用db.insert()
,它还将返回插入的行ID。不过,像现在这样使用编译语句会更快
如果您想尝试db.insert()
方法,它将如下所示:
ContentValues values = new ContentValues();
for (Student student: students) {
// use whatever constants you have for column names instead of these:
values.put(COLUMN_STUDENT_ID, student.getId());
values.put(COLUMN_STUDENT_FIRSTNAME, student.getFirstName());
values.put(COLUMN_STUDENT_LASTNAME, student.getLastName());
values.put(COLUMN_STUDENT_BIRTHDAY, student.getBirthday());
db.insert(StudentEntry.TABLE_NAME, null, values);
}
您可以执行以下操作,而不是
语句.execute()
。这将返回插入行的行ID。或者,正如@Tom所建议的,您可以使用db.insert()
,它还将返回插入的行ID。不过,像现在这样使用编译语句会更快
如果您想尝试db.insert()
方法,它将如下所示:
ContentValues values = new ContentValues();
for (Student student: students) {
// use whatever constants you have for column names instead of these:
values.put(COLUMN_STUDENT_ID, student.getId());
values.put(COLUMN_STUDENT_FIRSTNAME, student.getFirstName());
values.put(COLUMN_STUDENT_LASTNAME, student.getLastName());
values.put(COLUMN_STUDENT_BIRTHDAY, student.getBirthday());
db.insert(StudentEntry.TABLE_NAME, null, values);
}
db.insert()需要很多时间,因此我使用事务插入数据。但是,我可以在完成事务之前获取id值吗?是的,
SELECT LAST\u INSERT\u ROWID()
应该可以工作,即使在事务中-@Andrew顺便说一下,您应该能够在事务中使用db.INSERT()-它只是在后台编译一条语句。我如何使用db.INSERT()?你能举个例子吗?在我的答案中添加了一个例子。db.insert()需要很多时间,所以我使用事务插入数据。但是,我可以在完成事务之前获取id值吗?是的,SELECT LAST\u INSERT\u ROWID()
应该可以工作,即使在事务中-@Andrew顺便说一下,您应该能够在事务中使用db.INSERT()-它只是在后台编译一条语句。我如何使用db.INSERT()?你能举个例子吗?给我的答案加上一个例子。