我正在尝试使用this Hackage page中的sqlite-simple运行示例:
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Database.SQLite.Simple
main :: IO ()
main = do
conn <- open "test.db"
[[x]] <- query_ conn "select 2 + 2"
print x
但是我收到了这个错误:
Prelude> :load sqltest.hs
[1 of 1] Compiling Main ( sqltest.hs, interpreted )
sqltest.hs:9:12: error:
• Ambiguous type variable ‘a0’ arising from a use of ‘query_’
prevents the constraint ‘(Database.SQLite.Simple.FromField.FromField
a0)’ from being solved.
Probable fix: use a type annotation to specify what ‘a0’ should be.
These potential instances exist:
instance Database.SQLite.Simple.FromField.FromField Integer
-- Defined in ‘Database.SQLite.Simple.FromField’
instance Database.SQLite.Simple.FromField.FromField a =>
Database.SQLite.Simple.FromField.FromField (Maybe a)
-- Defined in ‘Database.SQLite.Simple.FromField’
instance Database.SQLite.Simple.FromField.FromField Bool
-- Defined in ‘Database.SQLite.Simple.FromField’
...plus five others
...plus 15 instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
• In a stmt of a 'do' block: [[x]] <- query_ conn "select 2 + 2"
In the expression:
do { conn <- open "test.db";
[[x]] <- query_ conn "select 2 + 2";
print x }
In an equation for ‘main’:
main
= do { conn <- open "test.db";
[[x]] <- query_ conn "select 2 + 2";
print x }
sqltest.hs:10:3: error:
• Ambiguous type variable ‘a0’ arising from a use of ‘print’
prevents the constraint ‘(Show a0)’ from being solved.
Relevant bindings include x :: a0 (bound at sqltest.hs:9:5)
Probable fix: use a type annotation to specify what ‘a0’ should be.
These potential instances exist:
instance Show Ordering -- Defined in ‘GHC.Show’
instance Show Integer -- Defined in ‘GHC.Show’
instance Show FormatError -- Defined in ‘Database.SQLite.Simple’
...plus 28 others
...plus 35 instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
• In a stmt of a 'do' block: print x
In the expression:
do { conn <- open "test.db";
[[x]] <- query_ conn "select 2 + 2";
print x }
In an equation for ‘main’:
main
= do { conn <- open "test.db";
[[x]] <- query_ conn "select 2 + 2";
print x }
Failed, modules loaded: none.
我使用Cabal安装了sqlite-simple。然后我尝试将示例代码加载到GHCI中。我是Haskell的新手,我只是在尝试简单的例子。
答案 0 :(得分:1)
您可以query
多种类型,因此不清楚x
的类型。所有GHC都知道x
是FromField
的实例(由query_
生成)和Show
的实例(由print
生成)。
您必须指定要查询的类型,例如
print (x :: Integer)
或
printInt :: Int -> IO ()
printInt = print
main = do
…
printInt x