SQL - Inserting a row and returning primary key

前端 未结 8 1151
南笙
南笙 2020-12-01 18:07

I have a little witty problem. Say, I inserted a row with some data in a table where a primary key is present. How would one \"SELECT\" the primary key of the row one just i

8条回答
  •  执笔经年
    2020-12-01 18:20

    For SQLite:

    SELECT [Column_1],  [Column_2],...  [Column_n]
    FROM [YourTable]
    WHERE rowid = (SELECT last_insert_rowid())
    

    whereas:

    • Column_1, Column_2,... Column_n: are the primary key of YourTable.

    If you'd created YourTable with primary key replaced rowid (i.e. one column pk defined as INTEGER PRIMARY KEY) you just use:

    SELECT last_insert_rowid()
    

    Which is a common case.
    Finally, this wont work for WITHOUT_ROWID tables.

    Please Check:

    https://www.sqlite.org/lang_corefunc.html#last_insert_rowid

提交回复
热议问题