r-dbi/DBI

Trying to `dbAppendTable` with an empty data frame throws "Error: Cannot pass NA to dbQuoteIdentifier()"

Kodiologist opened this issue · 3 comments

What I would've expected is for such a call to silently do nothing. Then, when the value argument to dbAppendTable is an expression yielding a data frame of 0 or more rows (and in the case of 0 rows, 0 columns as well), everything works as expected when 0 rows are produced. Even if you don't want to allow this, improving the error message is probably wise.

library(DBI)
db = dbConnect(RSQLite::SQLite(), ":memory:")
dbExecute(db, "create table T(n  integer primary key)")
dbAppendTable(db, "T", data.frame())

Thanks for raising this. We need to fix DBI::sqlAppendTableTemplate() to account for zero-length input.

You need to pass in columns, even if they have no rows:

library(DBI)
db <- dbConnect(RSQLite::SQLite(), ":memory:")
dbExecute(db, "create table T(n  integer primary key)")
#> [1] 0
dbAppendTable(db, "T", data.frame(n = integer()))
#> [1] 0

Created on 2021-01-02 by the reprex package (v0.3.0)

Working on improving the error message for your example.

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue and link to this old issue if necessary.