Copy data frames to database tables — dbWriteTable (original) (raw)

Writes, overwrites or appends a data frame to a database table, optionally converting row names to a column and specifying SQL data types for fields.

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.

Usage

dbWriteTable(conn, name, value, ...)

Arguments

conn

A DBIConnection object, as returned by[dbConnect()](dbConnect.html).

name

The table name, passed on to [dbQuoteIdentifier()](dbQuoteIdentifier.html). Options are:

value

A data.frame (or coercible to data.frame).

...

Other parameters passed on to methods.

Value

dbWriteTable() returns TRUE, invisibly.

Details

This function expects a data frame. Use [dbWriteTableArrow()](dbWriteTableArrow.html) to write an Arrow object.

This function is useful if you want to create and load a table at the same time. Use [dbAppendTable()](dbAppendTable.html) or [dbAppendTableArrow()](dbAppendTableArrow.html) for appending data to an existing table, [dbCreateTable()](dbCreateTable.html) or [dbCreateTableArrow()](dbCreateTableArrow.html) for creating a table, and [dbExistsTable()](dbExistsTable.html) and [dbRemoveTable()](dbRemoveTable.html) for overwriting tables.

DBI only standardizes writing data frames with dbWriteTable(). Some backends might implement methods that can consume CSV files or other data formats. For details, see the documentation for the individual methods.

Failure modes

If the table exists, and both append and overwrite arguments are unset, or append = TRUE and the data frame with the new data has different column names, an error is raised; the remote table remains unchanged.

An error is raised when calling this method for a closed or invalid connection. An error is also raised if name cannot be processed with [dbQuoteIdentifier()](dbQuoteIdentifier.html) or if this results in a non-scalar. Invalid values for the additional arguments row.names,overwrite, append, field.types, and temporary(non-scalars, unsupported data types,NA, incompatible values, duplicate or missing names, incompatible columns) also raise an error.

Additional arguments

The following arguments are not part of the dbWriteTable() generic (to improve compatibility across backends) but are part of the DBI specification:

They must be provided as named arguments. See the "Specification" and "Value" sections for details on their usage.

Specification

The name argument is processed as follows, to support databases that allow non-syntactic names for their objects:

The value argument must be a data frame with a subset of the columns of the existing table if append = TRUE. The order of the columns does not matter with append = TRUE.

If the overwrite argument is TRUE, an existing table of the same name will be overwritten. This argument doesn't change behavior if the table does not exist yet.

If the append argument is TRUE, the rows in an existing table are preserved, and the new data are appended. If the table doesn't exist yet, it is created.

If the temporary argument is TRUE, the table is not available in a second connection and is gone after reconnecting. Not all backends support this argument. A regular, non-temporary table is visible in a second connection, in a pre-existing connection, and after reconnecting to the database.

SQL keywords can be used freely in table names, column names, and data. Quotes, commas, spaces, and other special characters such as newlines and tabs, can also be used in the data, and, if the database supports non-syntactic identifiers, also for table names and column names.

The following data types must be supported at least, and be read identically with [dbReadTable()](dbReadTable.html):

Mixing column types in the same table is supported.

The field.types argument must be a named character vector with at most one entry for each column. It indicates the SQL data type to be used for a new column. If a column is missed from field.types, the type is inferred from the input data with [dbDataType()](dbDataType.html).

The interpretation of rownames depends on the row.names argument, see [sqlRownamesToColumn()](rownames.html) for details:

The default is row.names = FALSE.

See also

Other DBIConnection generics:[DBIConnection-class](DBIConnection-class.html),[dbAppendTable](dbAppendTable.html)(),[dbAppendTableArrow](dbAppendTableArrow.html)(),[dbCreateTable](dbCreateTable.html)(),[dbCreateTableArrow](dbCreateTableArrow.html)(),[dbDataType](dbDataType.html)(),[dbDisconnect](dbDisconnect.html)(),[dbExecute](dbExecute.html)(),[dbExistsTable](dbExistsTable.html)(),[dbGetException](dbGetException.html)(),[dbGetInfo](dbGetInfo.html)(),[dbGetQuery](dbGetQuery.html)(),[dbGetQueryArrow](dbGetQueryArrow.html)(),[dbIsReadOnly](dbIsReadOnly.html)(),[dbIsValid](dbIsValid.html)(),[dbListFields](dbListFields.html)(),[dbListObjects](dbListObjects.html)(),[dbListResults](dbListResults.html)(),[dbListTables](dbListTables.html)(),[dbQuoteIdentifier](dbQuoteIdentifier.html)(),[dbReadTable](dbReadTable.html)(),[dbReadTableArrow](dbReadTableArrow.html)(),[dbRemoveTable](dbRemoveTable.html)(),[dbSendQuery](dbSendQuery.html)(),[dbSendQueryArrow](dbSendQueryArrow.html)(),[dbSendStatement](dbSendStatement.html)(),[dbUnquoteIdentifier](dbUnquoteIdentifier.html)(),[dbWriteTableArrow](dbWriteTableArrow.html)()

Examples

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

dbWriteTable(con, "mtcars", mtcars[1:5, ])
dbReadTable(con, "mtcars")
#>    mpg cyl disp  hp drat    wt  qsec vs am gear carb
#> 1 21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
#> 2 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
#> 3 22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
#> 4 21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
#> 5 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2

dbWriteTable(con, "mtcars", mtcars[6:10, ], append = TRUE)
dbReadTable(con, "mtcars")
#>     mpg cyl  disp  hp drat    wt  qsec vs am gear carb
#> 1  21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
#> 2  21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
#> 3  22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
#> 4  21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
#> 5  18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
#> 6  18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
#> 7  14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
#> 8  24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
#> 9  22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
#> 10 19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4

dbWriteTable(con, "mtcars", mtcars[1:10, ], overwrite = TRUE)
dbReadTable(con, "mtcars")
#>     mpg cyl  disp  hp drat    wt  qsec vs am gear carb
#> 1  21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
#> 2  21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
#> 3  22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
#> 4  21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
#> 5  18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
#> 6  18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
#> 7  14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
#> 8  24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
#> 9  22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
#> 10 19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4

# No row names
dbWriteTable(con, "mtcars", mtcars[1:10, ], overwrite = TRUE, row.names = FALSE)
dbReadTable(con, "mtcars")
#>     mpg cyl  disp  hp drat    wt  qsec vs am gear carb
#> 1  21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
#> 2  21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
#> 3  22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
#> 4  21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
#> 5  18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
#> 6  18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
#> 7  14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
#> 8  24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
#> 9  22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
#> 10 19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4