简体   繁体   English

使用HDBC-ODBC对MS SQL进行排序查询

[英]Sequencing queries for MS SQL with HDBC-ODBC

What is the proper way to use a single connection and run multiple queries when connecting to MS SQL server from Haskell? 从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

With both the ODBC and FreeTDS drivers this query fails after processing a nondeterministic number of dates. 使用ODBC和FreeTDS驱动程序,此查询在处理了不确定的日期后将失败。 It also never prints any of the output sizes (10 expected) to stdout. 它还永远不会将任何输出大小(预期为10)打印到stdout。 Sample output: 样本输出:

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

Is this usage pattern incorrect? 此使用模式是否不正确?

System info: 系统信息:

$ 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

With both the ODBC and FreeTDS drivers this query fails after processing a nondeterministic number of dates. 使用ODBC和FreeTDS驱动程序,此查询在处理了不确定的日期后将失败。

This issue appears to have been reported as GitHub issue #28 for HDBC-OBCD . 该问题似乎已报告为HDBC-OBCD的GitHub问题#28 The workaround proposed there is to prepend DSN= to the connection string. 建议的解决方法是在连接字符串前添加DSN=

It also never prints any of the output sizes (10 expected) to stdout. 它还永远不会将任何输出大小(预期为10)打印到stdout。

The type of putStrLn . show . length <$> DB.quickQuery' conn q [] putStrLn . show . length <$> DB.quickQuery' conn q []的类型putStrLn . show . length <$> DB.quickQuery' conn q [] putStrLn . show . length <$> DB.quickQuery' conn q [] putStrLn . show . length <$> DB.quickQuery' conn q [] is IO (IO ()) . putStrLn . show . length <$> DB.quickQuery' conn q []IO (IO ()) You are creating an IO computation that prints the length, but you aren't actually having it executed . 您正在创建一个输出长度的IO计算,但实际上并没有执行它 To fix that, replace (<$>) with (>>=) , or something equivalent to it: 要解决此问题,请将(<$>)替换(<$>) (>>=)或类似的名称:

    DB.quickQuery' conn q [] >>= putStrLn . show . length 
    putStrLn . show . length =<< DB.quickQuery' conn q []
    rows <- DB.quickQuery' conn q []
    putStrLn . show . length $ rows

PS: putStrLn . show PS: putStrLn . show putStrLn . show can be more conveniently written as print . putStrLn . show可以更方便地写成print

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM