Aidan Reel wrote:

if you prepare the following statement

'insert into Table (id, id1) values( ?3, :id ); '

and then use sqlite_bind_parameter on the prepared statement you will receive the number 4. I appreciate where that answer is coming from, it takes the largest unnamed argument and adds the number of named arguments. I also appreciate that 3 is not got a very good number given it is the first one.


My question is, how do you find out the number of parameters in a statement that isn't prone to this.

Aidan,

I'm not sure I understand your question, but if you are trying to find the total number of parameters then you can use sqlite3_bind_parameter_count(). If you are trying to find out which of those parameters are actually referenced by an SQL statement, the only thing I can think of is to examine the output of the explain command.

For your example insert statement you get:

   SQLite version 3.3.5
   Enter ".help" for instructions
   sqlite> create table t (a,b);
   sqlite> .explain on
   sqlite> explain insert into t(a,b) values (?3, :id);
   addr  opcode          p1          p2          p3
   ----  --------------  ----------  ----------  ----------
   0     Goto            0           11
   1     Integer         0           0
   2     OpenWrite       0           2
   3     SetNumColumns   0           2
   4     NewRowid        0           0
   5     Variable        3           0           ?3
   6     Variable        4           0           :id
   7     MakeRecord      2           0           bb
   8     Insert          0           3           t
   9     Close           0           0
   10    Halt            0           0
   11    Transaction     0           1
   12    VerifyCookie    0           1
   13    Goto            0           1
   14    Noop            0           0
   sqlite>

Now you can count the unique variable numbers in the p1 column where the opcode is Variable. Unfortunately I don't think you can use SQL to do this directly since SQLite doesn't allow the explain keyword to be used as a subselect. It would be nice if this worked, but it doesn't.

   select count(distinct p1)
   from (explain insert into t(a,b) values(?3, :id))
   where opcode = 'Variable';

You will have to do the same thing in your calling code.

HTH
Dennis Cote

Reply via email to