我尝试使用 Haskell 中的 MySQL-simple 包从 MySQL 表中检索日期,但不断收到以下错误:
mysql-simple.hs:12:17:
No instance for (Database.MySQL.Simple.Result.Result Day)
arising from a use of `query_'
'record_date'是MySQL表中的一列,类型为Date,不允许为空。
我的源代码如下,任何人都可以阐明在 Haskell 中从 MySQL 检索日期的适当方法吗?
{-# LANGUAGE OverloadedStrings #-}
import Control.Monad
import Data.Text
import Data.Time.Clock (UTCTime)
import Data.Time.Calendar (Day)
import Data.ByteString (ByteString)
import Database.MySQL.Simple
main = do
conn <- connect defaultConnectInfo { connectUser = "haskell", connectPassword = "hpasswd", connectDatabase = "hdbase" }
rset <- query_ conn "select symbol, record_date, low, high, open, close from performance where symbol = 'AAPL' order by record_date desc limit 5"
forM_ rset $ \(s, r, l, h, o, c) -> do
print $ unpack s ++ (show (l::Double)) ++ (show (h::Double)) ++ (show (o::Double)) ++ (show (c :: Double))
print $ show (r :: Day)
最佳答案
经过进一步的探索并尝试执行 ghci 中的代码,我收到了一个错误,指出两个不同的库中存在重叠的定义,并且由于 GHCI 无法确定哪个是合适的,所以不得不放弃。消息:
GHCi runtime linker: fatal error: I found a duplicate definition for symbol
get_current_timezone_seconds
whilst processing object file
/home/user/.cabal/lib/time-1.5.0.1/ghc-7.6.3/HStime-1.5.0.1.o
This could be caused by:
* Loading two different object files which export the same symbol
* Specifying the same object file twice on the GHCi command line
* An incorrect `package.conf' entry, causing some object to be
loaded twice.
GHCi cannot safely continue in this situation. Exiting now. Sorry.
查看此错误之前的“加载包”语句,我注意到系统正在尝试加载 time-1.4.0.1,然后加载 time-1.5.0.1。当我一直试图找到访问 MySQL 的首选库时,我安装了 persist-mysql、hdbc-mysql 和其他几个库。反过来,我已经卸载了每个,但显然留下了一些残骸。删除 time-1.5.0.1 库后,我重新编译了原始代码并运行它,没有出现问题。
最后,似乎 ghc 输出了适当的错误消息,但我没有足够的信息来确定根本原因(即由 2 个不同版本的时间包带来的 2 个不同的 Day 定义)。不过现在一切都已修复...是的。
关于mysql - 使用 MySQL-simple 实例声明结果日,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29206614/