More comments ; exec functions throw exception in case of error

+ an example of SQL UPDATE using "exec()" shortcut
This commit is contained in:
Sébastien Rombauts
2012-12-10 15:14:05 +01:00
parent 8f17c9090b
commit e5c6e8dcca
3 changed files with 31 additions and 3 deletions

View File

@@ -156,9 +156,26 @@ int main (void)
db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, value TEXT)");
// first row
int nb = db.exec("INSERT INTO test VALUES (NULL, \"test\")");
std::cout << "INSERT INTO test VALUES (NULL, \"test\")\", returned " << nb << std::endl;
// second row
nb = db.exec("INSERT INTO test VALUES (NULL, \"second\")");
std::cout << "INSERT INTO test VALUES (NULL, \"second\")\", returned " << nb << std::endl;
// update the second row
nb = db.exec("UPDATE test SET value=\"second-updated\" WHERE id='2'");
std::cout << "UPDATE test SET value=\"second-updated\" WHERE id='2', returned " << nb << std::endl;
// Check the results : expect two row of result
SQLite::Statement query(db, "SELECT * FROM test");
std::cout << "SELECT * FROM test :\n";
while (query.executeStep())
{
std::cout << "row : (" << query.getColumn(0) << ", " << query.getColumn(1) << ")\n";
}
db.exec("DROP TABLE test");
}
catch (std::exception& e)
@@ -216,14 +233,13 @@ int main (void)
std::cout << "SQLite exception: " << e.what() << std::endl;
}
// Check the results
// Check the results (expect only one row of result, as the second one has been rollbacked by the error)
SQLite::Statement query(db, "SELECT * FROM test");
std::cout << "SELECT * FROM test :\n";
while (query.executeStep())
{
std::cout << "row : (" << query.getColumn(0) << ", " << query.getColumn(1) << ")\n";
}
}
catch (std::exception& e)
{