[英]Haskell HDBC.Sqlite3 fetchAllRows
Since I am an absolute Haskell beginner, but determined to conquer it, I am asking for help again. 由于我是绝对的Haskell初学者,但决心要征服它,因此我再次寻求帮助。
using: 使用:
fetchData2 = do
conn <- connectSqlite3 "dBase.db"
statement <- prepare conn "SELECT * FROM test WHERE id > 0"
execute statement []
results <- fetchAllRows statement
print results
returns: 收益:
[[SqlInt64 3,SqlByteString "Newco"],[SqlInt64 4,SqlByteString "Oldco"],[SqlInt64 5,SqlByteString "Mycom"],[SqlInt64 4,SqlByteString "Oldco"],[SqlInt64 5,SqlByteString "Mycom"]]
Is there a clever way to clean this data into Int
and [Char]
, in other words omitting types SqlInt64
and SqlByteString
. 是否有一种巧妙的方法将此数据清除为
Int
和[Char]
,换句话说,将类型SqlInt64
和SqlByteString
省略了。
You could define a helper: 您可以定义一个助手:
fetchRowFromSql :: Convertible SqlValue a => Statement -> IO (Maybe [a])
fetchRowFromSql = fmap (fmap (fmap fromSql)) . fetchRow
The implementation looks a bit daunting, but this is just because we need to drill down under the layered functors as you already noted (first IO
, then Maybe
and lastly []
). 该实现看起来有些令人生畏,但这仅仅是因为我们需要像您已经提到的那样深入挖掘分层函子(第一个
IO
,然后是Maybe
,最后一个[]
)。 This returns something that is convertible from a SqlValue
. 这将返回可从
SqlValue
转换的SqlValue
。 There are a bunch of these defined already. 已经定义了很多。 See eg the docs .
参见例如文档 。 An example (using
-XTypeApplications
): 一个示例(使用
-XTypeApplications
):
fetchRowFromSql @String :: Statement -> IO (Maybe [String])
I should perhaps add that the documentation mentions that fromSql
is unsafe. 我也许应该补充一点,文档中提到
fromSql
是不安全的。 Meaning that if you try to convert a sql value to an incompatible Haskell value the program will halt. 这意味着,如果您尝试将sql值转换为不兼容的Haskell值,则程序将停止。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.