Using SQLite to store data
SQLite is one of the most popular databases for compactly storing structured data. We will use the SQL binding for Haskell to store a list of strings.
Getting Ready
We must first install the SQLite3 database on our system. On Debian-based systems, we can issue the following installation command:
$ sudo apt-get install sqlite3
Install the SQLite package from cabal, as shown in the following command:
$ cabal install sqlite-simple
Create an initial database called test.db
that sets up the schema. In this recipe, we will only be storing integers with strings as follows:
$ sqlite3 test.db "CREATE TABLE test (id INTEGER PRIMARY KEY, str text);"
How to do it…
- Import the relevant libraries, as shown in the following code:
{-# LANGUAGE OverloadedStrings #-} import Control.Applicative import Database.SQLite.Simple import Database.SQLite.Simple.FromRow
- Create a
FromRow
typeclass implementation forTestField
, the data type we will be storing, as shown in the following...