Android Question How to know the key inserted?

Discussion in 'Android Questions' started by vecino, Jun 27, 2015.

  1. vecino

    vecino Well-Known Member Licensed User

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

    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]
     
    vecino likes this.
  3. vecino

    vecino Well-Known Member Licensed User

    Thank You. I got my head thinking multiuser :D
     
    LucaMs likes this.
  4. MaFu

    MaFu Well-Known Member Licensed User

    Or you use
    Code:
    Dim RowID As Int = SQL.ExecQuerySingleResult("SELECT last_insert_rowid() FROM [tbData]" )
     
    DonManfred, vecino and LucaMs like this.
  5. vecino

    vecino Well-Known Member Licensed User

    Great, thanks.
     
  6. cimperia

    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:

    Code:
    '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.
     
    vecino likes this.
  7. vecino

    vecino Well-Known Member Licensed User

    Of course :)
     
  8. MaFu

    MaFu Well-Known Member Licensed User

    But it doesn't matter the two statements differ because in multi-user environment both may fail.
     
    vecino likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice