Discussion:
[soci-users] Correct way of UPDATE statement
Christian Mol
2016-03-23 15:04:23 UTC
Permalink
Hi,

Using SOCI as the interface to an Sqlite3 database and a Postgres
database, I stumbled upon a "problem". I was using

try {
dbSession << "UPDATE categories SET name=:NAME,
description=:DESCRIPTION WHERE id=:ID", soci::use(updatedCategory);
}
catch (soci::soci_error& ex) {
// log error
}

When I try this statement with a value for :NAME that is already in
the table (UNIQUE column), I thought that until recently I got an
exception which could then be acted upon. However at the moment it
looks like the program just crashes on the dbSession << ... line.
If I however use

try{
soci::statement stUpdateCategory = (dbSession.prepare <<
"UPDATE categories SET name=:NAME, description=:DESCRIPTION
WHERE id=:ID", soci::use(updatedCategory));

stUpdateCategory.execute(true);
}
catch (soci::soci_error& ex) {
// log error
}

it seems to work correctly. My question now is, what is the correct
way to execute these statements that no really return data (INSERT,
DELETE etc)? When can I use the shorthand (once) notation?

Kind regards,
Christian Mol

Loading...