Search code examples
c#sqliteauto-increment

How to return autoincrement value in insert query in SQLite?


In my project I use System.Data.SQLite. Database has table Tags, which contains autoincrement primary field ID (type Integer). When I write:

using (SQLiteCommand command = conn.CreateCommand())
{
   command.CommandText = "insert into Tags(name) values(@name) returning into @id";
   command.Parameters.Add("@id", DbType.Int32).Direction = ParameterDirection.Output;
   command.ExecuteNonQuery();
}

Visual Studio said that the operation is not supported. How to fix it?

Error occurs on line:

command.Parameters.Add("@id", DbType.Int32).Direction = ParameterDirection.Output;

Solution

  • I found working query:

    SELECT last_insert_rowid()