liveBook · Manning

lastInsertRowId

As explained in Learn Haskell by Example

lastInsertRowId is a sqlite-simple function that returns the identifier assigned to the most recently inserted row on a connection.

lastInsertRowId returns the identifier assigned to the most recently inserted row on a database connection. With an INTEGER PRIMARY KEY column, it returns the new row’s identifier. The sqlite-simple function has type Connection -> Int64, so convert its result to Int when the surrounding API uses Int. section 16.3.3

Returning the new todo identifier

After inserting a TodoEntry inside withTransaction, bind newIdx <- lastInsertRowId dbConn and return fromIntegral newIdx. Wrapping the action with try produces Either SQLError Int, allowing callers to handle database errors or use the created identifier. section 16.3.3

How Learn Haskell by Example covers it

How deep it goes
Defines the return type, primary-key behavior, transaction placement, error wrapping, and required numeric conversion.
Examples
Demonstrates inserting a TodoEntry and returning its generated identifier from addEntry.
What it assumes
Assumes a Connection, an insertable TodoEntry, and familiarity with IO actions and SQLite primary keys.
What you can do after
Apply it via Database.SQLite.Simple to return a newly inserted row identifier from a transactional Haskell action.
How it connects
Connects database connections, SQL INSERT statements, SQLite transactions, primary keys, exceptions, and Int64 conversion.

This explainer was generated by Manning's knowledge graph from the text of Learn Haskell by Example. The section links point to the parts of the book it draws on.