Skip to content

Instantly share code, notes, and snippets.

@nurpax
Last active August 29, 2015 14:23
Show Gist options
  • Save nurpax/7dc329643277c3caac30 to your computer and use it in GitHub Desktop.
Save nurpax/7dc329643277c3caac30 to your computer and use it in GitHub Desktop.
sqlite-simple sample
{-# 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
-- 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: jjhellst@gmail.com
-- 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