如何将内存中的 dbplyr 数据库保存和加载到磁盘?

问题描述 投票:0回答:1

可以使用

dplyr
包创建内存数据库:

library(dplyr)
con <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")
copy_to(con, mtcars)

有没有办法将此数据库保存到文件并重新加载?

r database dplyr tidyverse in-memory-database
1个回答
0
投票

是的,您可以将数据保存到文件中的本地 SQLite 数据库中。

library(DBI)
library(RSQLite)

# create a connection to a local file named mtcars.db located in user's home folder
conn <- dbConnect(RSQLite::SQLite(), "~/mtcars.db")
# write mtcars data into the local SQLite file
dbWriteTable(conn, "mtcars", mtcars, overwrite = T)
# read the data from the database file into another variable
mtcars2 <-  dbReadTable(conn, "mtcars")
© www.soinside.com 2019 - 2024. All rights reserved.