Chris Swires | 12 Sep 2012 15:01
Picon
Picon
Favicon

Simple MySql query example request

Hi all, 

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. 

Best, 

Chris. 

Felipe Almeida Lessa | 12 Sep 2012 15:06
Picon
Gravatar

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.

Chris Swires | 12 Sep 2012 15:49
Picon
Picon
Favicon

Re: Simple MySql query example request

Thank you for the swift reply, from this example I get the error: 

`PersistEntityBackend' is not a (visible) associated type of class `persistent-0.9.0.4:Database.Persist.Store.PersistEntity'

Which is a new one to me, corrupted or outdated library in my installation perhaps?  I am still running Yesod 1.0, though as this is from the tutorial I doubt this is the issue. 

Best and thanks again.

Chris. 

On Wednesday, 12 September 2012 14:06:36 UTC+1, Felipe Lessa wrote:

On Wed, Sep 12, 2012 at 10:01 AM, Chris Swires <490...-QvkbJbr/y3w2EctHIo1CcQ@public.gmane.org> wrot e:
> 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.
Felipe Almeida Lessa | 12 Sep 2012 15:55
Picon
Gravatar

Re: Simple MySql query example request

On Wed, Sep 12, 2012 at 10:49 AM, Chris Swires <490415@...> wrote:
> Thank you for the swift reply, from this example I get the error:
>
> `PersistEntityBackend' is not a (visible) associated type of class
> `persistent-0.9.0.4:Database.Persist.Store.PersistEntity'
>
> Which is a new one to me, corrupted or outdated library in my installation
> perhaps?  I am still running Yesod 1.0, though as this is from the tutorial
> I doubt this is the issue.

You have two different versions of the persistent library installed,
so persistent-mysql may have been installed with a newer version of
persistent than you already had.  You may try

  $ cabal install yesod-platform-X.Y.Z persistent-mysql

where X.Y.Z is the version of the yesod-platform that you are using right now.

Cheers,

--

-- 
Felipe.


Gmane