Created
March 19, 2012 18:26
-
-
Save MgaMPKAy/2122913 to your computer and use it in GitHub Desktop.
persistent example
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 QuasiQuotes, TypeFamilies, GeneralizedNewtypeDeriving #-} | |
{-# LANGUAGE TemplateHaskell, OverloadedStrings, GADTs #-} | |
{-# LANGUAGE EmptyDataDecls #-} | |
{-# LANGUAGE FlexibleContexts #-} | |
{-# LANGUAGE TypeSynonymInstances #-} | |
{-# LANGUAGE FlexibleInstances #-} | |
import Database.Persist | |
import Database.Persist.TH | |
import Database.Persist.Sqlite | |
import Data.Time | |
import Control.Monad.IO.Class | |
share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persist| | |
Person sql=the-person-table | |
lastname String | |
firstname String sql=first_name | |
address String | |
city String | |
Name lastname firstname | |
|] | |
main :: IO () | |
main = withSqliteConn "test.db" $ runSqlConn $ do | |
runMigration migrateAll | |
insert $ Person "Adams" "John" "Oxford Street" "London" | |
insert $ Person "Bush" "George" "Fifth Avenue" "New York" | |
insert $ Person "Carter" "Thomas" "Changan Street" "Beijing" | |
insert $ Person "Carter" "William" "Xuanwumen 10" "Beijing" | |
result <- sqlQuery | |
liftIO $ print $ length result | |
liftIO $ print result | |
where | |
sqlQuery = selectList ([PersonLastname ==. "Carter"] ||. [PersonFirstname ==. "William"]) | |
[Asc PersonFirstname] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment