3

我正在使用 ODBC 连接到本地 SQL Server 进入 HDBC。

上连接后quickQuery,我不能close它。我需要执行commit第一个。

这是应该的方式吗?commit当我只执行查询时,为什么有必要?

在 GHCi 上:

m + Database.HDBC Database.HDBC.ODBC
conn <- connectODBC "Driver={SQL Server};Server=thiagon\\sqlserver2012;Database=senior;UID=framework;PWD=framework;"
vals <- quickQuery conn "SELECT TOP 5 * FROM whatever;" []
print vals
commit conn
disconnect conn

如果删除该commit conn行,则会出现异常:

*** Exception: SqlError {seState = "[\"25000\"]", seNativeError = -1, seErrorMsg = "disconnect: [\"0: [Microsoft][ODBC SQL Server Driver]Estado de transa\\65533\\65533o inv\\65533lido\"]"}

该消息是葡萄牙语,意思是“无效的交易状态”。

4

1 回答 1

1

A quickQuery could modify the table. I don't think the API analyses the string itself, or checks the database, to see whether or not the table was modified. And HDBC doesn't support autocommit.

You could use withTransaction, which will automatically handle this detail for you.

EDIT: Try using quickQuery', which is the strict version of quickQuery. In an example on http://book.realworldhaskell.org/read/using-databases.html (scroll down to ch21/query.hs), they didn't need a commit after a plain SELECT statement, but they were using quickQuery'.

于 2012-12-19T14:37:40.120 回答