liveBook · Manning

Database connection

As explained in Learn Haskell by Example

A database connection is an object representing an active communication channel to a database.

A database connection is an object representing an active communication channel to a database. In Haskell's sqlite-simple, open creates a Connection for a local database file or a temporary :memory: database. section 16.3.1

Use the connection as the first argument to database operations:

  • Run SELECT statements with query or query_, which return parsed rows. section 16.3.1
  • Run INSERT, UPDATE, and DELETE statements with execute, execute_, executeMany, or executeNamed. section 16.3.1
  • Bundle setup and application-specific actions in a database module whose functions receive the Connection. section 16.3.3
  • Protect multi-step changes with withTransaction and handle failures with try. section 16.3.3

An in-memory connection

dbConn <- open ":memory:"; execute_ dbConn "CREATE TABLE myTable (id INTEGER PRIMARY KEY, data TEXT)"; execute dbConn "INSERT INTO myTable VALUES (?,?)" (1 :: Int, "hello" :: String); query_ dbConn "SELECT * FROM myTable" returns [(1,"hello")].

How Learn Haskell by Example covers it

How deep it goes
Defines opening, query execution, transactions, error handling, and module organization without descending into connection internals.
Examples
Uses an in-memory SQLite table, parameterized insertion, row retrieval, batch updates, named parameters, and TodoEntry actions.
What it assumes
Assumes basic Haskell IO and SQL familiarity while demonstrating package setup, typed rows, parameterized queries, and error handling.
What you can do after
Operate/configure SQLite access by opening a connection and passing it to setup, query, modification, transaction, and retrieval functions.
How it connects
Connects sqlite-simple with SQLite databases, SQL queries, transactions, row conversion, and exception handling.

Also explained in

Compare how 6 books explain database connection →

Related concepts

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.