Note that there are some explanatory texts on larger screens.

plurals
  1. PODynamically Create SQLite In-Clause Parameters
    primarykey
    data
    text
    <p>I am trying to create a filter function for a web-based app that relies on SQLite for persistent storage. I am trying to use a prepared statement, but it has to be created dynamically based on the number of boxes checked by the user. I have an query created to which I dynamically add ? placeholders (using the suggestion from <a href="https://stackoverflow.com/questions/7418849/android-sqlite-in-clause-and-placeholders">IN clause and placeholders</a>). </p> <p>I've gotten as far as creating the correct number of ?s in the correct places, but I cannot figure out how to then add the values to the parameters inside the [...]'s. </p> <p>When I use a variable that holds all of the parameter values inside the [...], I get an error "number of '?'s in statement string does not match argument string". I've also tried using one variable for each of the sets of placeholders. That did not work, either. I suppose that the number of placeholders is checked against the number of parameters and an error returned before the variables are read.</p> <pre><code>// just spaced it with returns and removed the +'s and "'s to make it easier to read var filterQuery = SELECT t1.col1, t1.col2, t1.col3, t2.col2, t3.col2 FROM t1, t2, t3 WHERE t1.col2 = ? AND t1.col3 IN ( ?, ?, ?) AND t2.col2 IN ( ?, ?, ?, ?) </code></pre> <p>code:</p> <pre><code>db.transaction(function(tx) { tx.executeSql( filterQuery, [/* what do I put in here so that the number of ?s matches with the number of parameters? */], success, failure ); }); </code></pre> <p>How can I dynamically insert the parameters so that they match the number of placeholders and the mismatch error is avoided? </p> <p>Clarification: I have successfully run the queries without prepared statements. I'm hoping for a solution that will allow me to use them.</p> <p>(bonus question - will this query do what I am hoping it will do, i.e., grab only the rows that match all the selections that the user has made?)</p> <p>Thanks in advance!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload