Hi,

nice to hear, that you like tntdb. Thank you for using it.

Sorry to say that, but your problem is not that easy to solve. Placeholders in SQL statements are not just for stringwise replacement of something but placeholders for values. And only for single values.

You have to dynamically build your SQL-statement to set a list of values. I suggest to automatically build a sql statement with a suitable number of placeholders.

I think about something like that (untested):

template <typename T>
std::string placeholders(const T& c)
{
  std::ostringstream ret;
  for (unsigned n = 0; n < c.size(); ++n)
  {
    if (n > 0)
      ret << ", ";
    ret << ":value" << n;
  }
}

template <typename T>
std::string setPlaceholders(tntdb::Statement& stmt, const T& c)
{
  for (unsigned n = 0; n < c.size(); ++n)
  {
    std::ostringstream s;
    s << "value" << n;
    if (n > 0)
      ret << ", ";
    ret << "value" << n;
    stmt.set(s.str(), c[n]);
  }
}

...
    std::vector<int> values;
    values.push_back(17);
    values.push_back(18);
    values.push_back(34);
    tntdb::Statement stmt = db.prepare("select distinct entry_id"
                                       "       from index_terms"
                                       "         where entry_id in ("
                                      + placeholders(values) + ")");
    setPlaceholders(stmt, values);


Tommi


On 11/20/2011 06:25 PM, Carlos Franke wrote:
Dear Tntdb developers and users,

I am using Tntdb for a simple, local SQLite-based application, and so far, I am very happy with it. Thank you for this nice library! However, after months of developing with it, I have run into a problem which I cannot seem to resolve by myself. I would be glad if someone could give me some advice on it.

It is about a certain select statement including an IN-clause. It only works with a constant query statement like this: tntdb::Statement statement_with_var = db.prepare("select distinct entry_id" " from index_terms" " where entry_id in (1,2,3,8)");
...while the parameterised version does not yield any result:
tntdb::Statement statement_with_var = db.prepare("select distinct entry_id" " from index_terms" " where entry_id in (:subset)"); //followed by statement_with_var.set("subset", "1,2,3,8").select(), of course

There is a full example programme attached to this mail. It cannot be run without a fitting database of course, but maybe someone can tell where I am wrong just by looking at it.

Thank you for your time!

Carlos Franke


------------------------------------------------------------------------------
All the data continuously generated in your IT infrastructure
contains a definitive record of customers, application performance,
security threats, fraudulent activity, and more. Splunk takes this
data and makes sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-novd2d


_______________________________________________
Tntnet-general mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tntnet-general

------------------------------------------------------------------------------
All the data continuously generated in your IT infrastructure 
contains a definitive record of customers, application performance, 
security threats, fraudulent activity, and more. Splunk takes this 
data and makes sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-novd2d
_______________________________________________
Tntnet-general mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tntnet-general

Reply via email to