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



这段代码给了我一个错误:

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;

我在Google上什么也没找到。如果它很重要,重载字符串就会被启用。是否有解决方案(或方法来解决它)?

您正在使用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]])

最新更新