如何从tbl_dbi获取字段名称?

时间:2017-11-15 19:15:30

标签: r dbplyr

有没有办法直接从tbl_dbi对象(db_mtcars下方)获取字段名称?

 library(RSQLite)
 library(dbplyr)
 library(dplyr)
 con <- dbConnect(RSQLite::SQLite(), ":memory:")
 dbWriteTable(con, "mtcars", mtcars)

 db_mtcars <- tbl(con, "mtcars")

2 个答案:

答案 0 :(得分:3)

colnames()

> colnames(db_mtcars)
 [1] "mpg"  "cyl"  "disp" "hp"   "drat" "wt"   "qsec" "vs"   "am"   "gear"
[11] "carb"

答案 1 :(得分:2)

colnames(db_mtcars)是简单的解决方案,DBI::dbListFields(con, "mtcars")是一种奇特的解决方案。 即使names(db_mtcars)失败,两者都有效。