@hackage sql-simple0.1.0.0
common middle-level sql client.
Installation
Dependencies (6)
- base >=4.6 && <4.8
- exceptions >=0.6 && <0.7
- tagged >=0.7 && <0.8
- text >=1.1 && <1.2
- transformers >=0.4 && <0.5
- unordered-containers >=0.2 && <0.3 Show all…
Dependents (5)
@hackage/sql-simple-mysql, @hackage/sql-simple-sqlite, @hackage/acme-everything, @hackage/sql-simple-pool, @hackage/sql-simple-postgresql
sql-simple 
common middle-level sql client.
tutorial
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE ConstraintKinds #-}
import Control.Applicative
import Database.Sql.Simple
import Database.Sql.Simple.SQLite
import Database.Sql.Simple.PostgreSQL
-- you must specify 1st type variable of Sql Monad.
-- by explicit type signature,
testQuery :: (ToRow conn (Only Int), FromRow conn (Only Int), Backend conn)
=> conn -> Sql '[SQLite, PostgreSQL] [Int]
testQuery c = do
execute_ c "CREATE TABLE test (id int)"
execute c "INSERT INTO test VALUES (?)" (Only (1 :: Int))
map fromOnly <$> query_ c "SELECT * FROM test"
-- or sql function(testQuery' equivalent to testQuery).
testQuery' c = sql (sqlite +:+ postgreSQL) $ do
execute_ c "CREATE TABLE test (id int)"
execute c "INSERT INTO test VALUES (?)" (Only (1 :: Int))
i <- map fromOnly <$> query_ c "SELECT * FROM test"
return (i :: [Int])
-- you can specify backend specific Query.
specificQuery :: Backend conn => conn -> Sql '[SQLite, PostgreSQL] ()
specificQuery c =
execute_ c (specify sqlite "[sqlite query]" "[common query]")
main :: IO ()
main = do
l <- withConnection ("test.sqlite3" :: ConnectInfo SQLite) testQuery
-- l <- withConnection (def :: ConnectInfo PostgreSQL) testQuery
print l