2

我正在尝试使用这个 Hackage 页面中的 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 的新手,我只是在尝试简单的例子。

4

1 回答 1

1

您可以query使用多种类型,因此不清楚有哪些类型x。GHC 所知道的x是,它是FromField(due to query_) 的一个实例和 (due to ) 的一个Show实例print

您必须指定要查询的类型,例如

print (x :: Integer)

或者

printInt :: Int -> IO ()
printInt = print

main = do
  …
  printInt x
于 2017-05-31T12:15:44.590 回答