R: Execute a data manipulation statement on a given database...

dbSendStatement {DBI} | R Documentation

## Execute a data manipulation statement on a given database connection

### Description

The `dbSendStatement()` method only submits and synchronously executes the SQL data manipulation statement (e.g., `UPDATE`, `DELETE`, `INSERT INTO`, `DROP TABLE`, ...) to the database engine. To query the number of affected rows, call `dbGetRowsAffected()` on the returned result object. You must also call `dbClearResult()` after that. For interactive use, you should almost always prefer `dbExecute()`.

### Usage

    dbSendStatement(conn, statement, ...)

### Arguments

`conn` | A DBIConnection object, as returned by `dbConnect()`.
`statement` | a character string containing SQL.
`...` | Other parameters passed on to methods.

### Details

`dbSendStatement()` comes with a default implementation that simply forwards to `dbSendQuery()`, to support backends that only implement the latter.

### Value

`dbSendStatement()` returns an S4 object that inherits from DBIResult. The result set can be used with `dbGetRowsAffected()` to determine the number of rows affected by the query. Once you have finished using a result, make sure to clear it with `dbClearResult()`. An error is raised when issuing a statement over a closed or invalid connection, if the syntax of the statement is invalid, or if the statement is not a non-`NA` string.

### Specification

No warnings occur under normal conditions. When done, the DBIResult object must be cleared with a call to `dbClearResult()`. Failure to clear the result set leads to a warning when the connection is closed. If the backend supports only one open result set per connection, issuing a second query invalidates an already open result set and raises a warning. The newly opened result set is valid and must be cleared with `dbClearResult()`.

### See Also

For queries: `dbSendQuery()` and `dbGetQuery()`.

Other DBIConnection generics: `DBIConnection-class`, `dbDataType`, `dbDisconnect`, `dbExecute`, `dbExistsTable`, `dbGetException`, `dbGetInfo`, `dbGetQuery`, `dbIsValid`, `dbListFields`, `dbListResults`, `dbListTables`, `dbReadTable`, `dbRemoveTable`, `dbSendQuery`, `dbWriteTable`

### Examples

    con <- dbConnect(RSQLite::SQLite(), ":memory:")

    dbWriteTable(con, "cars", head(cars, 3))
    rs <- dbSendStatement(con,
      "INSERT INTO cars (speed, dist) VALUES (1, 1), (2, 2), (3, 3);")
    dbHasCompleted(rs)
    dbGetRowsAffected(rs)
    dbClearResult(rs)
    dbReadTable(con, "cars")   # there are now 6 rows

    dbDisconnect(con)

[Package DBI version 0.6-1 Index]