Skip to content

Commit

Permalink
Added Database and Statement method getChanges()
Browse files Browse the repository at this point in the history
Fix #331 How to get the number of updated/deleted rows?
  • Loading branch information
SRombauts committed Jul 25, 2021
1 parent 0c46d86 commit 18abb30
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
5 changes: 4 additions & 1 deletion include/SQLiteCpp/Database.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ class Database
void setBusyTimeout(const int aBusyTimeoutMs);

/**
* @brief Shortcut to execute one or multiple statements without results.
* @brief Shortcut to execute one or multiple statements without results. Return the number of changes.
*
* This is useful for any kind of statements other than the Data Query Language (DQL) "SELECT" :
* - Data Manipulation Language (DML) statements "INSERT", "UPDATE" and "DELETE"
Expand Down Expand Up @@ -404,6 +404,9 @@ class Database
*/
long long getLastInsertRowid() const noexcept;

/// Get number of rows modified by last INSERT, UPDATE or DELETE statement (not DROP table).
int getChanges() const noexcept;

/// Get total number of rows modified by all INSERT, UPDATE or DELETE statement since connection (not DROP table).
int getTotalChanges() const noexcept;

Expand Down
7 changes: 5 additions & 2 deletions include/SQLiteCpp/Statement.h
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ class Statement
int tryExecuteStep() noexcept;

/**
* @brief Execute a one-step query with no expected result.
* @brief Execute a one-step query with no expected result, and return the number of changes.
*
* This method is useful for any kind of statements other than the Data Query Language (DQL) "SELECT" :
* - Data Definition Language (DDL) statements "CREATE", "ALTER" and "DROP"
Expand All @@ -488,7 +488,7 @@ class Statement
*
* @return number of row modified by this SQL statement (INSERT, UPDATE or DELETE)
*
* @throw SQLite::Exception in case of error, or if row of results are returned !
* @throw SQLite::Exception in case of error, or if row of results are returned while they are not expected!
*/
int exec();

Expand Down Expand Up @@ -660,6 +660,9 @@ class Statement
const char * getColumnDeclaredType(const int aIndex) const;


/// Get number of rows modified by last INSERT, UPDATE or DELETE statement (not DROP table).
int getChanges() const noexcept;


////////////////////////////////////////////////////////////////////////////

Expand Down
8 changes: 7 additions & 1 deletion src/Database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ void Database::setBusyTimeout(const int aBusyTimeoutMs)
check(ret);
}

// Shortcut to execute one or multiple SQL statements without results (UPDATE, INSERT, ALTER, COMMIT, CREATE...).
// Shortcut to execute one or multiple SQL statements without results (UPDATE, INSERT, ALTER, COMMIT, CREATE...). Return the number of changes.
int Database::exec(const char* apQueries)
{
const int ret = tryExec(apQueries);
Expand Down Expand Up @@ -153,6 +153,12 @@ bool Database::tableExists(const char* apTableName)
long long Database::getLastInsertRowid() const noexcept
{
return sqlite3_last_insert_rowid(getHandle());
}

// Get number of rows modified by last INSERT, UPDATE or DELETE statement (not DROP table).
int Database::getChanges() const noexcept
{
return sqlite3_changes(getHandle());
}

// Get total number of rows modified by all INSERT, UPDATE or DELETE statement since connection.
Expand Down
8 changes: 7 additions & 1 deletion src/Statement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ bool Statement::executeStep()
return mbHasRow; // true only if one row is accessible by getColumn(N)
}

// Execute a one-step query with no expected result
// Execute a one-step query with no expected result, and return the number of changes.
int Statement::exec()
{
const int ret = tryExecuteStep();
Expand Down Expand Up @@ -308,6 +308,12 @@ const char * Statement::getColumnDeclaredType(const int aIndex) const
{
return result;
}
}

// Get number of rows modified by last INSERT, UPDATE or DELETE statement (not DROP table).
int Statement::getChanges() const noexcept
{
return sqlite3_changes(mStmtPtr);
}

int Statement::getBindParameterCount() const noexcept
Expand Down

0 comments on commit 18abb30

Please sign in to comment.