Re: Simple MySql query example request
On Wed, Sep 12, 2012 at 10:01 AM, Chris Swires <490415@...> wrote:
> Would someone be able to direct me to or provide a basic MySql query
> example? Literally just looking for a self contained connection, query,
> return result to page so that I can adapt this into my project. I've been
> attempting to do this myself but am unfortunately very strapped for time at
> the minute and so any help getting me started would be greatly appreciated.
>
> Thanks for any help in advance.
I didn't test this, but it should work:
{-# LANGUAGE QuasiQuotes, TemplateHaskell, TypeFamilies, OverloadedStrings #-}
{-# LANGUAGE GADTs, FlexibleContexts #-}
import Database.Persist
import Database.Persist.MySQL -- *
import Database.Persist.TH
import Control.Monad.IO.Class (liftIO)
share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persist|
Person
name String
age Int Maybe
deriving Show
BlogPost
title String
authorId PersonId
deriving Show
|]
main :: IO ()
main = withMySQLConn defaultConnectInfo $ runSqlConn $ do -- *
runMigration migrateAll
johnId <- insert $ Person "John Doe" $ Just 35
janeId <- insert $ Person "Jane Doe" Nothing
insert $ BlogPost "My fr1st p0st" johnId
insert $ BlogPost "One more for good measure" johnId
oneJohnPost <- selectList [BlogPostAuthorId ==. johnId] [LimitTo 1]
liftIO $ print (oneJohnPost :: [Entity BlogPost])
john <- get johnId
liftIO $ print (john :: Maybe Person)
delete janeId
deleteWhere [BlogPostAuthorId ==. johnId]
It's taken from persistent's chapter on the Yesod book. The only
lines I've changed are marked with "-- *".
Cheers,
--
--
Felipe.