2017-05-31 85 views
2

我試圖從this Hackage page運行使用SQLite,簡單的例子:哈斯克爾SQLite的,簡單類型的錯誤

{-# 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. 

我安裝使用驚天動地的sqlite-簡單。然後我試着將示例代碼加載到GHCI中。我是Haskell的新手,只是嘗試一些簡單的例子。

回答

1

你可以query很多類型,因此不清楚x是什麼類型。所有GHC知道約x是它的FromField(由於query_)和Show(由於print)的實例。

您必須指定您要查詢的類型,例如

print (x :: Integer) 

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

main = do 
    … 
    printInt x 
+0

這工作,謝謝。但接下來的問題是Hackage示例不正確?這裏最大的問題是我認爲示例代碼是絕對正確的。我應該更加謹慎,因爲我學習這門語言? – mitch

+0

從類型不明確的意義上講,這是不正確的。在查詢工作的意義上這是正確的。不過,我不確定這個軟件包是否是學習Haskell的典型出發點。 – Zeta