saveSQLite: Save R data frame to SQLite file. This function saves a data...

View source: R/sqlite.R

saveSQLiteR Documentation

Save R data frame to SQLite file. This function saves a data frame to an SQLite database file. It can create indices on specified columns for faster querying. The database file is stored in the task's binary directory.

Description

Save R data frame to SQLite file. This function saves a data frame to an SQLite database file. It can create indices on specified columns for faster querying. The database file is stored in the task's binary directory.

Usage

saveSQLite(
  object,
  task,
  type,
  subdir = NULL,
  dirCreate = TRUE,
  index = NULL,
  tableName = "data",
  overwrite = FALSE,
  append = FALSE,
  row.names = FALSE
)

Arguments

object

data.frame to serialize.

task

Object of class D4TAlinkTask, as created by initTask.

type

Filename type. If the type is an array, the cocatenation of the elements is used with separator"-". Filenames have the form [task name]_[type].[ext]

subdir

(optional) Subdirectory.

dirCreate

Logical, if TRUE (by default) the directory is created.

index

character vector of column names to create indices on.

tableName

name of the table in the SQLite database.

overwrite

if TRUE, overwrite existing table.

append

if TRUE, append to existing table.

row.names

if TRUE, save row names as a column.

Value

The file name invisibly.

Examples

## Not run: 
task <- loadTask("myproject","mypackage","mytask")
# create a sample data frame
df <- data.frame(a = 1:5, b = letters[1:5])
# save it to SQLite
fn <- saveSQLite(df, task, "testdb",tableName="table1",
                 index = c("a"))
# read it back
df_loaded <- readSQLite(task,"testdb",tableName="table1")
# print the loaded data frame
print(df_loaded)
# add second data frame
df2 <- data.frame(a = 6:10, b = letters[6:10])
saveSQLite(df2, task,"testdb",tableName="table2")
# query the data
query_result <- querySQLite("SELECT * FROM table2 WHERE a > 8",
                            task,"testdb")
print(query_result)                           
#' # append to the first table
saveSQLite(df2, task,"testdb",tableName="table1",append=TRUE)
# read the updated table
df_loaded <- readSQLite(task,"testdb",tableName="table1")
# print the updated data frame
print(df_loaded)
# cleanup
unlink(fn)

## End(Not run) 

D4TAlink.light documentation built on Sept. 11, 2025, 1:07 a.m.