SQLite Forum

sqlite3_bind_() problem I do not understand

sqlite3_bind_() problem I do not understand

(1) By anonymous on 2021-04-12 04:02:57 [link] [source]

Hi, I am new to writing code using the amalgamation in c++ builder.

My problem is this, I send this sql "SELECT Name, Composer FROM tracks WHERE Name LIKE :p0 AND AlbumId = :p1" to  

int rc = sqlite3_prepare_v2(_db, UTF8String(sql).c_str(), -1, &stmt, NULL);

this returns SQLITE_OK, after this, without explaining everything, Id o the following  

for(int i = 0; i < c; i++)
  _val = Params[i]->ParamValue;
  dataType = op->GetSQLiteBindTypeValue(Params[i]->ColumnType);
  rc = Bind(dataType /*tText*/, stmt, i+1, UTF8String(_val).c_str());
  _expandedStmt = sqlite3_expanded_sql(stmt);

Ok, this works fine for the number of parameters added to a vector of parameters, column data types are pre determined and works correctly, dataType reflects the column type from the database itself.

the Bind( ... ) method works fine it returns SQLITE_OK as does the actual binding statement used in the Bind ( ... ) method i.e;

rc = sqlite3_bind_text( stmt, valueCount, (char*)value.c_str(), -1, SQLITE_STATIC); 

I acknowledge that this may not be correct and that is why I am here but, my issue is that calling sqlite3_expanded_sql(stmt);

shows something like this;

"SELECT Name, Composer FROM tracks WHERE Name LIKE '98' AND AlbumId = 98"

This is not correct as both parameter markers have been replaced with the same last value,

Stepping through the code at run time, binding the first value is ok and shows

"SELECT Name, Composer FROM tracks WHERE Name LIKE 'p%' AND AlbumId = NULL"

it is on the 2nd bind operation that the first parameter place holder gets the same value as the last, I can't work out why so, HELP, what am I doing wrong??

(2) By Dan Kennedy (dan) on 2021-04-12 04:56:30 in reply to 1 [source]

value.c_str() changing? Try binding with SQLITE_TRANSIENT to ensure SQLite takes a copy of the string when it is bound.

(3) By anonymous on 2021-04-12 07:31:57 in reply to 2 [link] [source]

Thank you Dan,

that was it, much appreciated, I have lot's to learn.