Last active
August 29, 2015 14:23
-
-
Save nurpax/7dc329643277c3caac30 to your computer and use it in GitHub Desktop.
sqlite-simple sample
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
{-# LANGUAGE OverloadedStrings #-} | |
import Control.Applicative | |
import qualified Data.Text as T | |
import Database.SQLite.Simple | |
import Database.SQLite.Simple.FromRow | |
data TestField = TestField Int T.Text deriving (Show) | |
instance FromRow TestField where | |
fromRow = TestField <$> field <*> field | |
instance ToRow TestField where | |
toRow (TestField id_ str) = toRow (id_, str) | |
main :: IO () | |
main = do | |
conn <- open "test.db" | |
execute_ conn "CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY, str TEXT)" | |
execute conn "INSERT INTO test (str) VALUES (?)" (Only ("test string 2" :: String)) | |
execute conn "INSERT INTO test (id, str) VALUES (?,?)" (TestField 13 "test string 3") | |
rowId <- lastInsertRowId conn | |
executeNamed conn "UPDATE test SET str = :str WHERE id = :id" [":str" := ("updated str" :: T.Text), ":id" := rowId] | |
r <- query_ conn "SELECT * from test" :: IO [TestField] | |
mapM_ print r | |
execute conn "DELETE FROM test WHERE id = ?" (Only rowId) | |
close conn |
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
-- Initial sqlite-simple-test.cabal generated by cabal init. For further | |
-- documentation, see http://haskell.org/cabal/users-guide/ | |
name: sqlite-simple-test | |
version: 0.1.0.0 | |
license: AllRightsReserved | |
author: Janne Hellsten | |
maintainer: [email protected] | |
-- copyright: | |
-- category: | |
build-type: Simple | |
-- extra-source-files: | |
cabal-version: >=1.10 | |
executable sqlite-simple-test | |
main-is: gistfile1.hs | |
-- other-modules: | |
-- other-extensions: | |
build-depends: base >=4.6 && <4.7 | |
, sqlite-simple | |
, text | |
-- hs-source-dirs: | |
default-language: Haskell2010 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment