RSQLite (version 0.8-4)

sqliteCopyDatabase: Copy a SQLite database

Description

This function copies a database connection to a file or to another database connection. It can be used to save an in-memory database (created using dbname = ":memory:") to a file or to create an in-memory database as a copy of anothe database.

Usage

sqliteCopyDatabase(from, to)

Arguments

from
A SQLiteConnection object. The main database in from will be copied to to.
to
Either a string specifying the file name where the copy will be written or a SQLiteConnection object pointing to an empty database. If to specifies an already existing file, it will be overwritten without a warning.

Value

  • Returns NULL.

Details

This function uses SQLite's experimental online backup API to make the copy.

References

http://www.sqlite.org/backup.html

Examples

Run this code
## Create an in memory database
db <- dbConnect(SQLite(), dbname = ":memory:")
df <- data.frame(letters=letters[1:4], numbers=1:4, stringsAsFactors = FALSE)
ok <- dbWriteTable(db, "table1", df, row.names = FALSE)
stopifnot(ok)

## Copy the contents of the in memory database to
## the specified file
backupDbFile <- tempfile()
sqliteCopyDatabase(db, backupDbFile)
diskdb <- dbConnect(SQLite(), dbname = backupDbFile)
stopifnot(identical(df, dbReadTable(diskdb, "table1")))

## Copy from one connection to another
db2 <- dbConnect(SQLite(), dbname = ":memory:")
sqliteCopyDatabase(db, db2)
stopifnot(identical(df, dbReadTable(db2, "table1")))

## cleanup
dbDisconnect(db)
dbDisconnect(diskdb)
dbDisconnect(db2)
unlink(backupDbFile)

Run the code above in your browser using DataCamp Workspace