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

@ -67,6 +67,8 @@ public:
* @param[in] apQueries one or multiple UTF-8 encoded, semicolon-separate SQL statements
*
* @return number of changes made by those SQL statements
*
* @throw SQLite::Exception in case of error
*/
int exec(const char* apQueries); // throw(SQLite::Exception);
@ -84,6 +86,10 @@ public:
* @see also Statement class for handling queries with multiple results
*
* @param[in] apQuery an UTF-8 encoded SQL query
*
* @return a temporary Column object with the first column of result.
*
* @throw SQLite::Exception in case of error
*/
Column execAndGet(const char* apQuery); // throw(SQLite::Exception);
@ -93,6 +99,10 @@ public:
* Table names are case sensitive.
*
* @param[in] apTableName an UTF-8 encoded case sensitive Table name
*
* @return true if the table exists.
*
* @throw SQLite::Exception in case of error
*/
bool tableExists(const char* apTableName); // throw(SQLite::Exception);

View File

@ -114,12 +114,14 @@ public:
/**
* @brief Execute a step of the query to fetch one row of results.
*
* @see Database::exec() is a shortcut to execute one or multiple statements without results
*
* @return - true (SQLITE_ROW) if there is another row ready : you can call getColumn(N) to get it
* then you have to call executeStep() again to fetch more rows until the query is finished
* - false (SQLITE_DONE) if the query has finished executing : there is no (more) row of result
* (case of a query with no result, or after N rows fetched successfully)
*
* @see Database::exec() is a shortcut to execute one or multiple statements without results
* @throw SQLite::Exception in case of error
*/
bool executeStep(void); // throw(SQLite::Exception);

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)
{