Created
July 29, 2012 09:24
-
-
Save nurpax/3197006 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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