0

I am facing an issue with executing SQL query in C library for SQLite. Here issue is when query contains IN clause.

Eg.,

Query:

SELECT * FROM EMPLOYEE WHERE ID IN (?);

C:

sqlite3_stmt *elems_stmt;
char empIds[40];
...
...
...
sqlite3_bind_text(elems_stmt, 1, (const char *)empIds, -1, SQLITE_TRANSIENT);

Here empIds could be 4,5,6

But as result, elems_stmt not getting any results.

Instead, if I hard code the values in the query as

SELECT * FROM EMPLOYEE WHERE ID IN (4,5,6);

I am able to see the results.

Please let me know what is missing in the first query and corresponding code.

Thanks.

Dai
  • 141,631
  • 28
  • 261
  • 374
Omkar Shetkar
  • 3,488
  • 5
  • 35
  • 50

1 Answers1

1

Thats not possible in sqLite, assuming you have a variable number of values in your IN statement. Instead, you can just clip together the query as a string with other program logic and then execute it like you would any hardcoded one.

Magisch
  • 7,312
  • 9
  • 36
  • 52
  • Thanks. Done a workaround. Since I know the maximum number of values that can occur with IN statement, I will hard code those many place holders in the query. Assign relevant number of values and remaining will be assigned with default values. SELECT * FROM (?,?,?,?) will be assigned values (4,5,6,-1) where -1 indicating default value. – Omkar Shetkar Oct 27 '15 at 02:42