Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get the number of updated/deleted rows? #331

Closed
paulocoutinhox opened this issue Jul 16, 2021 · 4 comments · Fixed by #333
Closed

How to get the number of updated/deleted rows? #331

paulocoutinhox opened this issue Jul 16, 2021 · 4 comments · Fixed by #333
Assignees

Comments

@paulocoutinhox
Copy link
Contributor

Hi,

How i can get the number of affected rows for update/delete?

Example:

auto sql =
    "UPDATE todo SET "
    "title = :title, "
    "body = :body, "
    "data = :data, "
    "done = :done, "
    "updated_at = :updated_at "
    "WHERE id = :id";

auto application = std::static_pointer_cast<ApplicationCoreImpl>(ApplicationCore::shared());
auto db = application->getDB();

SQLite::Statement query(*db, sql);

query.bind(":id", id);
query.bind(":title", todo.title);
query.bind(":body", todo.body);
query.bind(":data", MapHelper::toJsonString(todo.data));
query.bind(":done", todo.done);
query.bind(":updated_at", DateTime::getStringFromDateTime(todo.updatedAt));
query.exec();

Thanks.

@paulocoutinhox
Copy link
Contributor Author

There is any utility for this?

sqlite3_changes(db->getHandle())

If not, can you add a function like this ?

int getTotalChanges() const noexcept; // nothrow

@paulocoutinhox
Copy link
Contributor Author

You can add this:

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

@SRombauts SRombauts self-assigned this Jul 25, 2021
@SRombauts
Copy link
Owner

Hello @paulo-coutinho,

Your query.exec() call does already return the number of changes made :)

 * @return number of row modified by this SQL statement (INSERT, UPDATE or DELETE)
 */
int Statement::exec()

SRombauts added a commit that referenced this issue Jul 25, 2021
Fix #331 How to get the number of updated/deleted rows?
SRombauts added a commit that referenced this issue Jul 25, 2021
Fix #331 How to get the number of updated/deleted rows?

Fix cpplint warnings about line size with a NOLINT comment when better to keep oneline
SRombauts added a commit that referenced this issue Jul 25, 2021
…s() from SRombauts/get-changes

Fix #331 How to get the number of updated/deleted rows?
@SRombauts
Copy link
Owner

Thanks for the suggestion,

I implemented it and added it in examples and unit tests, even if it wasn't really strictly needed; it shows people might look for it like you did.

Cheers!
Sébastien

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants