Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Microsoft ODBC Driver 13: "Invalid character value for cast specification" inserting SqlBool #35

Open
mclark1129 opened this issue Mar 24, 2018 · 0 comments

Comments

@mclark1129
Copy link

I seem to be having issues trying to insert a Bool value into a bit field in MSSQL. I'm receiv'ing the following error when I execute the INSERT:

SqlError {seState = "["22018"]", seNativeError = -1, seErrorMsg = "execute execute: ["0: [Microsoft][ODBC Driver 13 for SQL Server]Invalid character value for cast specification"]"}

I have no issues reading out bit values using SELECT, although it looks like these values are being pulled into a SqlChar instead of a SqlBool so not sure if that's a clue. The SqlChar is converted to Bool on my record type, so at least it's compatible.

When I convert my record type into [SqlValue], the value for the Bool is represented as a SqlBool False instead of SqlChar '\NUL' and this fails on INSERT.

Below is some sample code that illustrates the issue:

type ItemId = Integer
data TodoItem = TodoItem { itemId :: ItemId, description :: String, dueDate :: LocalTime, completed :: Bool } deriving (Show, Generic)
instance FromJSON TodoItem
instance ToJSON TodoItem

-- Maps the record type to SqlValues
fromItem :: TodoItem -> [SqlValue]
fromItem = flip (\x -> map (\f -> f x)) $ [toSql . description, toSql . dueDate, toSql . completed]

insertItem :: IConnection conn => IO conn -> TodoItem -> IO ()
insertItem ioconn item = do
    conn <- ioconn
    stmt <- prepare conn "INSERT INTO Items (Description, DueDate, Completed) VALUES (?, ?, ?)"
    execute stmt $ fromItem item
    commit conn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant