作者:OP尋一 | 来源:互联网 | 2023-01-07 17:29
所以我正在尝试在Haskell中编写一个数据库层,以与SQLite DB进行交互.我在关于数据库的章节中遵循了Real World Haskell一书中的指示.这是我到目前为止:
{-# LANGUAGE FlexibleContexts #-}
import Database.HDBC
import Database.HDBC.Sqlite3
db = "dev.db"
cOnn= connectSqlite3 db
getPerson person =
quickQuery' conn "select name from person where name like ?" [toSql person]
main :: IO ()
main = do
print $ getPerson "Michael"
但我得到错误:
Could not deduce (IConnection (IO Connection))
arising from a use of ‘quickQuery'’
from the context: convertible-1.1.1.0:Data.Convertible.Base.Convertible
a SqlValue
bound by the inferred type of
getPerson :: convertible-1.1.1.0:Data.Convertible.Base.Convertible
a SqlValue =>
a -> IO [[SqlValue]]
作为Haskell初学者,我真的不明白.我可以在getPerson上添加一个类型注释,但这似乎并没有解决它.
1> wizzup..:
此代码已编译
{-# LANGUAGE FlexibleContexts #-}
import Database.HDBC
import Database.HDBC.Sqlite3
import Data.Convertible.Base
db :: String
db = "dev.db"
getPerson :: (Convertible a SqlValue, IConnection conn) => conn -> a -> IO [[SqlValue]]
getPerson conn person =
quickQuery' conn "select name from person where name like ?" [toSql person]
main :: IO ()
main = do
conn <- connectSqlite3 db
person <- getPerson conn "Michael"
print person
connectSqlite3 :: FilePath -> IO Connection
所以conn
应该提取的价值IO monad
.
也许有更好的方法来写这个使用bind
传递连接quickQuery
但我不知道,我从来没有使用过这个库,只是从类型中猜测ghci