mirror of
https://github.com/allankoechke/SQLiteCpp.git
synced 2026-12-01 09:08:47 +00:00
More comments ; exec functions throw exception in case of error
+ an example of SQL UPDATE using "exec()" shortcut
This commit is contained in:
@@ -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)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user