I am trying to query some data from a MSSQL database through HDBC and ODBC. I have however run into a problem when trying to query data from a table with unicode in the column names.
Consider the following MWE:
mwe :: IConnection conn => conn -> IO [[SqlValue]]
mwe conn =
do r <- quickQuery' conn
"SELECT [Højde] FROM [Table]"
[]
return r
When executing the above and passing it a connection object to the database i get the following error message:
*** Exception: SqlError {seState = "[\"42S22\",\"42000\"]", seNativeError = -1, seErrorMsg = "execute execute: [\"207: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Invalid column name 'H\\195\\184jde'.\",\"8180: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Statement(s) could not be prepared.\"]"}
The relevant part most likely being that H\\195\\184jde is not a valid column name.
My research has mostly led to results about unicode in the parameters to the query. I have tried to use bytestrings instead of normal strings, but since the argument for QuickQuery' is a string that did not help.
I don't have an MS SQL instance to test this, but the code in
HDBC-odbcencodes the query using UTF-8. Meanwhile, this documentation suggests that for modern ODBC drivers, the character set used for queries depends on the process locale at the time the driver is initialized. If it's"C", which is the usual process default, then the driver will use the UTF-8 character set. However, if the process executes:before initializing the driver, and the current Windows locale is, say, English using the Latin-1 1252 code page, then the ODBC driver will expect Latin-1 encoded queries. Unfortunately for you, the GHC runtime does run
setlocale(LC_ALL,""), so that's probably what's going wrong.If you reset the locale to
"C"at the start of"main", that may fix the issue:I'm not sure if this will cause other problems (e.g., with terminal input/output). If it does, you may be able to set the locale to
"C"before initializing the driver and then reset it to""right after.