Problem with single quotes Swift/MacOS
(1) By frameworker74 on 2021-04-03 13:13:17 [link]
Hi folks, I have trouble to execute a statement with single quotes like this one: ``` rc = sqlite3_prepare_v2(db, "select strftime('%s') as time, int_test from stest", -1, &ppStmt, &pzTail); ``` rc returns 0 but a subsequent ``` let ct = String(cString: sqlite3_column_decltype(ppStmt, cc)).uppercased(); ``` runs into Unexpectedly found nil while implicitly unwrapping an Optional value It seems ppStmt is not set on this moment. I could catch it, however, but in the end my SQl would fail. Replacing the statement to a command without single quotes works fine instead. Generally there's seems a problem with single quotes of parameters in functions passed by the command in Swift. In the case above a Bind would not work because we're not talking about a Where clause. Any help appreciate.
(2.1) By Larry Brasfield (larrybr) on 2021-04-03 13:51:02 edited from 2.0 in reply to 1
From <u>[Declared Datatype Of A Query Result](https://sqlite.org/c3ref/column_decltype.html)</u>: "If the Nth column of the result set is an expression or subquery, then a NULL pointer is returned." Regarding: > It seems ppStmt is not set on this moment. I could catch it, however, but in the end my SQl would fail. If the prepare succeeded, you got a sqlite3_stmt object back. > Replacing the statement to a command without single quotes works fine instead. Generally there's seems a problem with single quotes of parameters in functions passed by the command in Swift. In the case above a Bind would not work because we're not talking about a Where clause. I don't know what to make of those assertions. I can only guess what "command without single quotes" means, and whether your "works fine" resembles mine is similarly uncertain. I'm not sure what you mean by "Bind", but binding parameter values within prepared SQL statements is certainly allowed outside of WHERE clauses.
(3) By Keith Medcalf (kmedcalf) on 2021-04-03 13:48:47 in reply to 1 [link]
What is the value of cc? I suspect that it is 0 and that this Swift thing simply cannot deal with empty results since column 0 has no declared type. What do you mean do "command without single quotes". Do you mean, for example, the following statement "select datetime() as time, int_test from stest" behaves differently? This is probably an issue with this Swift thing since the C API does not have this problem.
(4) By anonymous on 2021-04-03 14:36:20 in reply to 1 [link]
In swift `sqlite3_column_decltype` is imported as `func sqlite3_column_decltype(_: OpaquePointer!, _: Int32) -> UnsafePointer<Int8>!`. It returns an implicitly unwrapped optional which in this case is `nil`, so calling `uppercased()` on `nil` is causing the problem.
(5) By frameworker74 on 2021-04-03 15:58:39 in reply to 4 [link]
Ok, your're right. I read here: https://www.sqlite.org/c3ref/column_decltype.html. strftime() is an expression, so decltype returns NULL and therefore cc is set to nil which in turn throws the error.