database – Haskell / Persistent-Sqlite:“没有实例(Control.Monad.Trans.Resource.MonadResource IO)”

前端之家收集整理的这篇文章主要介绍了database – Haskell / Persistent-Sqlite:“没有实例(Control.Monad.Trans.Resource.MonadResource IO)”前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我一直试图想出一种简单直观的方法来使用 Haskell数据库.我从 Yesod book获取了这些代码,并尝试将其清理干净,以便更容易理解和使用.
{-# LANGUAGE QuasiQuotes,TemplateHaskell,TypeFamilies,OverloadedStrings #-}
{-# LANGUAGE GADTs,FlexibleContexts #-}

import Database.Persist
import Database.Persist.sqlite (withsqliteConn,runsqlConn,runMigration)
import Database.Persist.TH (share,mkPersist,mkMigrate,sqlSettings,persist)

share [mkPersist sqlSettings,mkMigrate "migrateAll"] [persist|
Person                              -- Table name
    name String                     -- String value
    age Int Maybe                   -- Numerical value
|]

updateDB x y = withsqliteConn "data.db" $runsqlConn $do
    runMigration migrateAll         -- Creates "Person" table if one doesn't exist
    insert $Person x $Just y      -- Inserts values into .db file

main = do
    updateDB "Frank Silver" 40      -- adds name "Frank Silver" and age "40" to data.db file

这段代码几乎可以工作,但我得到以下错误,我无法解决.

No instance for (Control.Monad.Trans.Resource.MonadResource IO)
      arising from a use of `updateDB'
    Possible fix:
      add an instance declaration for
      (Control.Monad.Trans.Resource.MonadResource IO)
    In a stmt of a 'do' block: updateDB "Frank Silver" 40
    In the expression: do { updateDB "Frank Silver" 40 }
    In an equation for `main': main = do { updateDB "Frank Silver" 40 }

任何建议指向我正确的方向将不胜感激.

main = do
    updateDB "Frank Silver" 40

updateDB“Frank Silver”40的类型被推断为IO(),因为这是main的默认类型(对于某些a,它必须具有类型IO a).但是从定义来看,它的类型被推断为MonadRescource m => m a for some a(可能是a =(),但我不确定),并且没有实例MonadResource IO.因此,您需要将updateDB转换为IO操作,正常的方法是runResourceT,它将ResourceT m a转换为m a(此处为m = IO),因此

main = runResourceT $updateDB "Frank Silver" 40

作品.

原文链接:https://www.f2er.com/sqlite/197689.html

猜你在找的Sqlite相关文章