没有Database.SQLite.Simple.FromField.FromField Char的实例

时间:2015-08-19 14:27:41

标签: sqlite haskell

此代码给出了一个错误:

67:    isFileInstalled f = do
68:                dbcon <- open "/var/lib/beaver/pkgs.db"
69:                res <- queryNamed dbcon "SELECT * FROM files WHERE path = :path" [":path" := ("/usr/bin/getpkg" :: String)]
70:                mapM_ putStrLn res
71:                -- when (null res) (return ())
72:                -- putStrLn ("File " ++ res ++ " is already installed and comes from " ++ res ++ ".") -- first res is `owner` and second is `path`
73:                close dbcon
74:                exit

我收到此错误:

No instance for (Database.SQLite.Simple.FromField.FromField Char)
     arising from a use of `queryNamed'    In a stmt of a 'do' block:
     res <- queryNamed
              dbcon
              "SELECT * FROM files WHERE path = :path"
              [":path" := ("/usr/bin/getpkg" :: String)]    In the expression:
     do { dbcon <- open "/var/lib/beaver/pkgs.db";
          res <- queryNamed
                   dbcon
                   "SELECT * FROM files WHERE path = :path"
                   [":path" := ("/usr/bin/getpkg" :: String)];
          mapM_ putStrLn res;
          close dbcon;
          .... }    In an equation for `isFileInstalled':
       isFileInstalled f
         = do { dbcon <- open "/var/lib/beaver/pkgs.db";
                res <- queryNamed
                         dbcon
                         "SELECT * FROM files WHERE path = :path"
                         [":path" := ("/usr/bin/getpkg" :: String)];
                mapM_ putStrLn res;

我在谷歌找不到任何东西。如果它很重要,则启用OverloadedStrings。是否有解决方法(或修复方法)?

1 个答案:

答案 0 :(得分:1)

您使用res作为[String]

mapM_ putStrLn res

相当于[[Char]]。但queryNamed返回行列表,因此每行都有[Char]类型。有一个instance FromField a => FromRow [a],但没有instance FromField Char。这就是错误消息所说的内容。

我从未使用sqlite-simple,但很明显每行包含许多字段,因此您尝试获取包含多个Char类型字段的行。这是没有意义的。根据实际的数据库方案,每行应该是例如许多文本字段,res可以有[[String]]类型。在这种情况下,您应该像下一个一样使用它:

mapM_ (mapM_ putStrLn) (res :: [[String]])