Using OTL library (c++) to INSERT without binding parameters

534 Views Asked by At

I am attempting to use OTL to do a basic insert into a sql server db. For this insert I don't need to bind any parameters.

otl_stream o_stream;
std::string query = "INSERT INTO common VALUES (1,2,3);";
o_stream.open(1, query.c_str(), db_);
o_stream.flush();
o_stream.close();

However, even after flushing and closing the otl_stream, the db is locked on that table (can't read via separate application). I have to close my otl application to unlock the table.

If I parameterize the insert statement then everything works as it should (inserts successful, no table lock).

otl_stream o_stream;
std::string query = "INSERT INTO common VALUES (1,2,a:<int>);";
o_stream.open(1, query.c_str(), db_);
o_stream << 3;

That's not ideal, since ideally I'd like to avoid parameterizing/binding if it's not necessary. Any suggestions?

EDIT:

Answer Below

1

There are 1 best solutions below

0
On

From the author of the OTL library:

otl_streams are meant to be reused, that is, a INSERT statement stream needs to have at least one bind variable to be "flushable". For cases when there is 0 bind variables, there is this: http://otl.sourceforge.net/otl3_const_sql.htm.

Missing from that link is the required db.commit() call. Without the commit call the table will be locked.

Solution for the example given in the question:

std::string query = "INSERT INTO common VALUES (1,2,3);";
db_.direct_exec(query.c_str());
db_.commit();