Are you sure that any of your SQLite calls are successful? You should initialize result
to nil
so that your function returns nil
if any errors are caught.
Three (probably related) issues with your code:
The index to
sqlite3_column_text
should be zero-based; you're passing2
, which should refer to the third column. You probably mean to pass1
. From the docs:...the second argument is the index of the column for which information should be returned. The leftmost column of the result set has the index 0.
You really shouldn't use
SELECT *
. Specify the columns you want!You should specialize your query by binding values, not by concatenating strings! Your code is rife with the possibility of SQL injections (not to mention incorrect queries).
For example (with no error checking):
const char *query = "SELECT * FROM ? WHERE ?=?";
sqlite3_stmt *compiledQuery;
sqlite3_prepare_v2(database, query, -1, &compiledQuery, NULL);
sqlite3_bind_text(compiledQuery, 1, "Lessons", -1, SQLITE_TRANSIENT);
sqlite3_bind_text(compiledQuery, 2, "Chrono", -1, SQLITE_TRANSIENT);
sqlite3_bind_text(compiledQuery, 3, "0001", -1, SQLITE_TRANSIENT);
Note that the index here is 1-based (I don't know why they do that). From the docs:
The second argument is the index of the SQL parameter to be set. The leftmost SQL parameter has an index of 1.