SQLite数据库中的Unicode文本似乎被打破了

时间:2012-07-07 21:47:05

标签: sqlite haskell unicode utf-8

我正在使用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和编码的内容?

1 个答案:

答案 0 :(得分:1)

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

writeIt content = withFile "test.txt" WriteMode (\handle -> hSetBinaryMode handle True >> mapM_ (\(c:a:t:[]) -> hPutStrLn handle c) content)