Mono.Data.Sqlite on Monotouch: How to get the value of an autoincrement field after inserting a row? Mono.Data.Sqlite on Monotouch: How to get the value of an autoincrement field after inserting a row? sqlite sqlite

Mono.Data.Sqlite on Monotouch: How to get the value of an autoincrement field after inserting a row?


SQLite has some internal core functions. One of these functions is last_insert_rowid(). So all you have to do is to issue a command "SELECT last_insert_rowid()". Example in Monotouch:

    public long GetLastInsertRowId(SqliteConnection connection)    {        // Assuming connection is an open connection from your INSERT        using (SqliteCommand command = new SqliteCommand("SELECT last_insert_rowid()", connection))        {            return (long)command.ExecuteScalar();        }    }

Or you can combine the select with your insert, example:

string SqlCommand = "INSERT into Customers ([Name], .... [City]) VALUES (@Name, ... @City);SELECT last_insert_rowid();";