This method returns the number of rows that were added, deleted, or updated by a data manipulation statement.
Methods in other packages
This documentation page describes the generics. Refer to the documentation pages linked below for the documentation for the methods that are implemented in various backend packages.
DatabaseConnector::dbGetRowsAffected("DatabaseConnectorDbiResult")
DatabaseConnector::dbGetRowsAffected("DatabaseConnectorJdbcResult")
sparklyr::dbGetRowsAffected("DBISparkResult")
Arguments
- res
An object inheriting from DBIResult.
- ...
Other arguments passed on to methods.
Value
dbGetRowsAffected()
returns a scalar number (integer or numeric),
the number of rows affected by a data manipulation statement
issued with dbSendStatement()
.
The value is available directly after the call
and does not change after calling dbFetch()
.
NA_integer_
or NA_numeric_
are allowed if the number of rows affected is not known.
For queries issued with dbSendQuery()
,
zero is returned before
and after the call to dbFetch()
.
NA
values are not allowed.
The command execution flow
This section gives a complete overview over the flow
for the execution of SQL statements that have side effects
such as stored procedures, inserting or deleting data,
or setting database or connection options.
Most of this flow, except repeated calling of dbBindArrow()
,
is implemented by dbExecute()
, which should be sufficient
for non-parameterized queries.
This flow requires an active connection established by dbConnect()
.
See also vignette("dbi-advanced")
for a walkthrough.
Use
dbSendStatement()
to create a result set object of class DBIResult. For some queries you need to passimmediate = TRUE
.Optionally, bind query parameters with
dbBind()
ordbBindArrow()
. This is required only if the query contains placeholders such as?
or$1
, depending on the database backend.Optionally, use
dbGetRowsAffected()
to retrieve the number of rows affected by the query.Repeat the last two steps as necessary.
Use
dbClearResult()
to clean up the result set object. This step is mandatory even if no rows have been fetched or if an error has occurred during the processing. It is good practice to useon.exit()
orwithr::defer()
to ensure that this step is always executed.
Failure modes
Attempting to get the rows affected for a result set cleared with
dbClearResult()
gives an error.
See also
Other DBIResult generics:
DBIResult-class
,
dbBind()
,
dbClearResult()
,
dbColumnInfo()
,
dbFetch()
,
dbGetInfo()
,
dbGetRowCount()
,
dbGetStatement()
,
dbHasCompleted()
,
dbIsReadOnly()
,
dbIsValid()
,
dbQuoteLiteral()
,
dbQuoteString()
Other command execution generics:
dbBind()
,
dbClearResult()
,
dbExecute()
,
dbSendStatement()
Examples
con <- dbConnect(RSQLite::SQLite(), ":memory:")
dbWriteTable(con, "mtcars", mtcars)
rs <- dbSendStatement(con, "DELETE FROM mtcars")
dbGetRowsAffected(rs)
#> [1] 32
nrow(mtcars)
#> [1] 32
dbClearResult(rs)
dbDisconnect(con)