$ mkdir takusen $ cd takusen $ darcs get http://darcs.haskell.org/takusen $ ghc --make Setup $ Setup configure $ Setup build $ Setup install
Next, we will create a sqlite3 database with a simple table tbl1:
$ mkdir sqlite_demo
$ cd sqlite_demo
$ sqlite3 demo.db
SQLite version 3.5.6
Enter ".help" for instructions
sqlite> create table tbl1 (key TEXT, value TEXT);
sqlite> insert into table tbl1 values('k1', 'v1');
sqlite> insert into table tbl1 values('k2', 'v2');
sqlite> insert into table tbl1 values('k3', 'v3');
sqlite> .quit
Create sqlite_demo.hs and enter this:
{-# OPTIONS -fglasgow-exts #-}
{-# OPTIONS -fallow-overlapping-instances #-}
module Main (main) where
import Database.Sqlite.Enumerator
import Control.Monad.Trans (liftIO)
import System.Environment
query1Iteratee :: (Monad m) => String -> String -> IterAct m [(String, String)]
query1Iteratee a b accum = result' ((a,b):accum)
main :: IO ()
main = do
flip catchDB reportRethrow $
withSession (connect "demo.db") (do
let iter (s::String) (_::String) = result s
result <- doQuery (sql "select * from tbl1") query1Iteratee []
liftIO $ putStrLn $ show result
)
Compile with the following command and run:
$ ghc --make sqlite_demo -o sqlite_demo $ ./sqlite_demoYou should get this:
[("k3","v3"),("k2","v2"),("k1","v1")]
