SQLite Forum

App using SQlite C API faces segmentation fault
Login
A sidebar unrelated to the question but related to the demonstrated code:

> if (queryStmt.find("SELECT ") >= 0) ...

That's a very fragile way to check for whether a statement is capable of fetching data, for various reasons. Much more robust and generic is:

- Prepare the query, regardless of what type you *think* it is based on string-sniffing.
- Use [sqlite3_column_count()](https://www.sqlite.org/c3ref/column_count.html) to get the query's result column count: if it's 0, the statement is non-fetching (INSERT, UPDATE, etc.), else it is a SELECT, a SELECT-like pragma (if there are any such pragmas), or some such hypothetical construct.
- Step the cursor one time. That will execute an INSERT/UPDATE/etc. the same as it will a SELECT, but it will return `SQLITE_DONE` instead of `SQLITE_ROW`.

The notable functional difference there from your current code is that `sqlite3_exec()` will compile and run all of the statements in the given string, whereas prepare/step will only handle the first statement (noting that the final argument to the step family of functions can be used to get the next position in the SQL so that the caller can loop over the string to handle each statement it includes).