使用单个连接并在Haskell连接到MS SQL Server时运行多个查询的正确方法是什么?
import qualified Database.HDBC as DB
import qualified Database.HDBC.ODBC as DB
import Data.Time.Calendar (fromGregorian)
import Control.Monad (forM_)
testQueries :: String -> IO ()
testQueries connectionString =
do
conn <- DB.connectODBC connectionString
forM_ [startDate..endDate] $ date -> do
putStrLn $ "processing date: " ++ show date
putStrLn . show . length <$> DB.quickQuery' conn q []
DB.disconnect conn
where
q :: String
q =
"SELECT TOP 10 * FROM INFORMATION_SCHEMA.COLUMNS"
startDate =
fromGregorian 2016 9 14
endDate =
fromGregorian 2016 11 29
使用ODBC和Freetds驱动程序,此查询在处理了不确定的日期后失败。它也永远不会打印出任何输出大小(预期10(。样本输出:
processing date: 2016-09-14
processing date: 2016-09-15
processing date: 2016-09-16
processing date: 2016-09-17
processing date: 2016-09-18
processing date: 2016-09-19
processing date: 2016-09-20
processing date: 2016-09-21
processing date: 2016-09-22
processing date: 2016-09-23
processing date: 2016-09-24
processing date: 2016-09-25
processing date: 2016-09-26
processing date: 2016-09-27
processing date: 2016-09-28
exe: Prelude.chr: bad argument: 5832776
这种用法模式不正确?
系统信息:
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 17.10
Release: 17.10
Codename: artful
$ cat /etc/odbcinst.ini
[ODBC Driver 17 for SQL Server]
Description=Microsoft ODBC Driver 17 for SQL Server
Driver=/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.0.so.1.1
UsageCount=1
[FreeTDS]
Description = FreeTDS
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
Setup = /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so
UsageCount = 1
使用ODBC和Freetds驱动程序,此查询在处理了不确定的日期后失败。
此问题似乎已报告为HDBC-OBCD的GitHub问题28。提出的解决方法是将DSN=
预先置于连接字符串。
它也永远不会打印任何输出大小(预期10(。
putStrLn . show . length <$> DB.quickQuery' conn q []
的类型是IO (IO ())
。您正在创建一个打印长度的IO
计算,但实际上并未执行它。要解决此问题,请用(>>=)
替换(<$>)
,或等同的内容:
DB.quickQuery' conn q [] >>= putStrLn . show . length
putStrLn . show . length =<< DB.quickQuery' conn q []
rows <- DB.quickQuery' conn q []
putStrLn . show . length $ rows
P.S。:putStrLn . show
可以更方便地写成print
。