Reputation: 5351
I'm giving my first steps into HDBC using ODBC to connect to a local SQL Server.
After a quickQuery
on the connection, I can't close
it. I need to perform a commit
first.
Is this the way it is supposed to be? Why is the commit
necessary when I'm only performing a query?
On 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
If I remove the commit conn
line, I get an exception:
*** Exception: SqlError {seState = "[\"25000\"]", seNativeError = -1, seErrorMsg = "disconnect: [\"0: [Microsoft][ODBC SQL Server Driver]Estado de transa\\65533\\65533o inv\\65533lido\"]"}
The message is in portuguese, it means "invalid transaction state".
Upvotes: 3
Views: 724
Reputation: 8136
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'
.
Upvotes: 1