1

我正在使用 http://hackage.haskell.org/package/sqlite-0.5.2.2绑定到 SQLite 数据库。在 *.db 文件中有 UTF-8 编码的文本,我可以在文本编辑器和 sqlite CLI 工具中保证这一点。

连接到数据库并检索数据时 - 文本内容已损坏。简单的测试如下:

import qualified Database.SQLite as SQL
import Control.Applicative ((<$>))
import System.IO

buildSkypeMessages dbh = 
  (go <$> (SQL.execStatement dbh "select chatname,author,timestamp,body_xml from messages order by chatname, timestamp")) >>=
  writeIt
  where
    writeIt content = withFile "test.txt" WriteMode (\handle -> mapM_ (\(c:a:t:[]) -> hPutStrLn handle c) content)
    go (Left msg) = fail msg
    go (Right rows) = map f $ concat rows
      where
        f' (("chatname",SQL.Text chatname):
            ("author",SQL.Text author):
            ("timestamp",SQL.Int timestamp):
            r) = ([chatname, author], r)
        f xs = let (partEntry, (item:_)) = f' xs
               in case item of
                 ("body_xml",SQL.Text v) -> v:partEntry
                 ("body_xml",SQL.Null)   -> "":partEntry
        escape (_,SQL.Text v) = v
        escape (_,SQL.Null) = ""
        escape (_,SQL.Int v) = show v

那里可能有什么问题?我是否缺少 Sqlite 或 Haskell I/O 和编码的东西?

4

1 回答 1

1

实际上,问题与 SQLite 绑定无关,而是与 Haskell 中的字符串处理有关。是什么解决了这个问题 - 在将数据放在句柄上之前调用 hSetBinaryMode :

writeIt content = withFile "test.txt" WriteMode (\handle -> hSetBinaryMode handle True >> mapM_ (\(c:a:t:[]) -> hPutStrLn handle c) content)
于 2012-07-09T07:12:15.797 回答