Skip to content

Instantly share code, notes, and snippets.

@nurpax
Created July 29, 2012 09:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nurpax/3197006 to your computer and use it in GitHub Desktop.
Save nurpax/3197006 to your computer and use it in GitHub Desktop.
import Data.Either
import Database.SQLite
{-
SCHEMA
CREATE TABLE test (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name VARCHAR(32)
);
-}
-- NOTE I don't get the sqlite package types for execStatement. Why
-- does it return a list of list of Rows instead of a list of Rows. A
-- Row is already a list of column names and values. Thus using
-- 'head' here to to the actual rows list.
printRows :: [[Row String]] -> IO ()
printRows rows = mapM_ p (head rows)
where
p :: Row String -> IO ()
p c = print (c :: Row String)
main :: IO ()
main = do
conn <- openConnection "test.sqlite"
-- Use basic exec param to insert
execParamStatement_ conn "INSERT INTO test (name) VALUES (:n)" [(":n", Text "mike")]
-- Same with insertRow
insertRow conn "test" [("name", "michael")]
-- Take the last insert ID for the previous insert
lastId <- getLastRowID conn
putStrLn ("Last inserted row id: "++show lastId)
-- Read the rows
rowse <- execStatement conn "SELECT * from test"
printRows (either (const []) id rowse)
closeConnection conn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment