Android Question How to know the key inserted?

vecino

Well-Known Member
Licensed User
Hello, I have a table. example:
B4X:
create table tbData
(
  id integer not null primary key autoincrement,
  name varchar(64) default '' 
);
If I do this:
B4X:
SQL.ExecNonQuery2("insert into tbData values(null,?)",Array As String(edName.Text))
How do I know the "id" inserted?
 

LucaMs

Expert
Licensed User
SQL.ExecQuery("SELECT MAX(id) FROM tbData")

given that autoincrement adds 1 to last id inserted.

[Dim Cur As Cursor...etc, of course]
 

cimperia

Active Member
Licensed User
I suppose that it won’t make any difference as I understand the app is running in a single user environment, but it were not, these two statements would not be equivalent:

B4X:
'1
SELECT MAX(id) FROM tbData

'2
SELECT last_insert_rowid() FROM tbData


The first one will returns the current maximum id of the table, whereas the second one returns the last id inserted within the session. They would match in a single user environment, not necessarily in a multi-user one.
 

MaFu

Well-Known Member
Licensed User
I suppose that it won’t make any difference as I understand the app is running in a single user environment, but it were not, these two statements would not be equivalent:

B4X:
'1
SELECT MAX(id) FROM tbData

'2
SELECT last_insert_rowid() FROM tbData


The first one will returns the current maximum id of the table, whereas the second one returns the last id inserted within the session. They would match in a single user environment, not necessarily in a multi-user one.
But it doesn't matter the two statements differ because in multi-user environment both may fail.
 
Top