Title: | Enhanced Reading and Writing for 'SQLite' Databases |
---|---|
Description: | Reads and writes data frames to 'SQLite' databases while preserving time zones (for POSIXct columns), projections (for 'sfc' columns), units (for 'units' columns), levels (for factors and ordered factors) and classes for logical, Date and 'hms' columns. It also logs changes to tables and provides more informative error messages. |
Authors: | Joe Thorley [aut, cre] , Sebastian Dalgarno [ctb] , Poisson Consulting [cph, fnd] |
Maintainer: | Joe Thorley <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.2.0.9006 |
Built: | 2024-12-09 01:26:04 UTC |
Source: | https://github.com/poissonconsulting/readwritesqlite |
chk_sqlite_conn
checks if a SQLite connection.
chk_sqlite_conn(x, connected = NA, x_name = NULL) check_sqlite_connection( x, connected = NA, x_name = substitute(x), error = TRUE )
chk_sqlite_conn(x, connected = NA, x_name = NULL) check_sqlite_connection( x, connected = NA, x_name = substitute(x), error = TRUE )
x |
The object to check. |
connected |
A logical scalar specifying whether x should be connected. |
x_name |
A string of the name of object x or NULL. |
error |
A flag specifying whether to through an error if the check fails. |
NULL
, invisibly. Called for the side effect of throwing an error
if the condition is not met.
check_sqlite_connection()
: Check SQLite Connection
conn <- rws_connect() chk_sqlite_conn(conn) rws_disconnect(conn) try(chk_sqlite_conn(conn, connected = TRUE))
conn <- rws_connect() chk_sqlite_conn(conn) rws_disconnect(conn) try(chk_sqlite_conn(conn, connected = TRUE))
Opens a SQLiteConnection to a SQLite database with foreign key constraints enabled.
rws_connect(dbname = ":memory:", exists = NA)
rws_connect(dbname = ":memory:", exists = NA)
dbname |
The path to the database file. SQLite keeps each database instance in one single file. The name of the database is the file name, thus database names should be legal file names in the running platform. There are two exceptions:
|
exists |
A flag specifying whether the table(s) must already exist. |
A SQLiteConnection to a SQLite database with foreign key constraints enabled.
conn <- rws_connect() print(conn) rws_disconnect(conn)
conn <- rws_connect() print(conn) rws_disconnect(conn)
An sf tibble of example data.
rws_data
rws_data
An object of class tbl_df
(inherits from tbl
, data.frame
) with 3 rows and 6 columns.
rws_data
rws_data
Add Descriptions to SQL Meta Data Table
rws_describe_meta(x, ..., conn)
rws_describe_meta(x, ..., conn)
x |
An object specifying the descriptions. |
... |
Not used. |
conn |
A SQLiteConnection to a database. |
An invisible copy of the updated meta table.
Other rws_describe_meta:
rws_describe_meta.character()
Add Descriptions to SQL Meta Data Table
## S3 method for class 'character' rws_describe_meta(x, column, description, ..., conn)
## S3 method for class 'character' rws_describe_meta(x, column, description, ..., conn)
x |
A character vector of table name(s). |
column |
A character vector of column name(s). |
description |
A character vector of the description(s) |
... |
Not used. |
conn |
A SQLiteConnection to a database. |
An invisible copy of the updated meta table.
Other rws_describe_meta:
rws_describe_meta()
conn <- rws_connect() rws_write(rws_data, exists = FALSE, conn = conn) rws_read_meta(conn) rws_describe_meta("rws_data", "Units", "The site length.", conn = conn) rws_describe_meta("rws_data", "POSIXct", "Time of the visit", conn = conn) rws_read_meta(conn) rws_disconnect(conn)
conn <- rws_connect() rws_write(rws_data, exists = FALSE, conn = conn) rws_read_meta(conn) rws_describe_meta("rws_data", "Units", "The site length.", conn = conn) rws_describe_meta("rws_data", "POSIXct", "Time of the visit", conn = conn) rws_read_meta(conn) rws_disconnect(conn)
Add Data Frame of Descriptions to SQL Meta Data Table
## S3 method for class 'data.frame' rws_describe_meta(x, ..., conn)
## S3 method for class 'data.frame' rws_describe_meta(x, ..., conn)
x |
A data frame with columns Table, Column, Description. |
... |
Not used. |
conn |
A SQLiteConnection to a database. |
An invisible character vector of the previous descriptions.
Other rws_read:
rws_read()
,
rws_read.SQLiteConnection()
,
rws_read.character()
Closes a SQLiteConnection to a SQLite database.
rws_disconnect(conn)
rws_disconnect(conn)
conn |
An |
conn <- rws_connect() rws_disconnect(conn) print(conn)
conn <- rws_connect() rws_disconnect(conn) print(conn)
Drops SQLite table using DROP TABLE.
rws_drop_table(table_name, conn)
rws_drop_table(table_name, conn)
table_name |
A string of the name of the table. |
conn |
A SQLiteConnection to a database. |
Also drops rows from meta and init tables.
TRUE
https://www.sqlite.org/lang_droptable.html
Other rws_rename:
rws_rename_column()
,
rws_rename_table()
conn <- rws_connect() rws_write(rws_data, exists = FALSE, conn = conn) rws_list_tables(conn) rws_drop_table("rws_data", conn = conn) rws_list_tables(conn) rws_disconnect(conn)
conn <- rws_connect() rws_write(rws_data, exists = FALSE, conn = conn) rws_list_tables(conn) rws_drop_table("rws_data", conn = conn) rws_list_tables(conn) rws_disconnect(conn)
Export all spatial datasets in a database as geopackages.
rws_export_gpkg(conn, dir, overwrite = FALSE)
rws_export_gpkg(conn, dir, overwrite = FALSE)
conn |
A SQLiteConnection to a database. |
dir |
A string of the path to the directory to save the geopackages in. |
overwrite |
A flag specifying whether to overwrite existing geopackages. |
If more than one spatial column is present in a table, a separate geopackage will be exported for each, and the other spatial columns will be dropped.
An invisible named vector of the file names and new file names saved.
Gets the table names excluding the names of the meta and log tables.
rws_list_tables(conn)
rws_list_tables(conn)
conn |
A SQLiteConnection to a database. |
A character vector of table names.
conn <- rws_connect() rws_list_tables(conn) rws_write(rws_data, exists = FALSE, conn = conn) rws_list_tables(conn) rws_disconnect(conn)
conn <- rws_connect() rws_list_tables(conn) rws_write(rws_data, exists = FALSE, conn = conn) rws_list_tables(conn) rws_disconnect(conn)
Gets a query from a SQLite database.
rws_query(query, meta = TRUE, conn)
rws_query(query, meta = TRUE, conn)
query |
A string of a SQLite query. |
meta |
A flag specifying whether to preserve meta data. |
conn |
A SQLiteConnection to a database. |
A data frame of the query.
conn <- rws_connect() rws_write(rws_data, exists = FALSE, conn = conn) rws_query("SELECT date, posixct, factor FROM rws_data", conn = conn) rws_disconnect(conn)
conn <- rws_connect() rws_write(rws_data, exists = FALSE, conn = conn) rws_query("SELECT date, posixct, factor FROM rws_data", conn = conn) rws_disconnect(conn)
Read from a SQLite Database
rws_read(x, ...)
rws_read(x, ...)
x |
An object specifying the table(s) to read. |
... |
Not used. |
A named list of data frames.
Other rws_read:
rws_describe_meta.data.frame()
,
rws_read.SQLiteConnection()
,
rws_read.character()
The table is created if it doesn't exist.
rws_read_init(conn)
rws_read_init(conn)
conn |
A SQLiteConnection to a database. |
A data frame of the init table
conn <- rws_connect() rws_read_init(conn) rws_write(rws_data, exists = FALSE, conn = conn) rws_read_init(conn) rws_disconnect(conn)
conn <- rws_connect() rws_read_init(conn) rws_write(rws_data, exists = FALSE, conn = conn) rws_read_init(conn) rws_disconnect(conn)
The table is created if it doesn't exist.
rws_read_log(conn)
rws_read_log(conn)
conn |
A SQLiteConnection to a database. |
A data frame of the log table
conn <- rws_connect() rws_read_log(conn) rws_write(rws_data, exists = FALSE, conn = conn) ## Not run: rws_read_log(conn) ## End(Not run) rws_disconnect(conn)
conn <- rws_connect() rws_read_log(conn) rws_write(rws_data, exists = FALSE, conn = conn) ## Not run: rws_read_log(conn) ## End(Not run) rws_disconnect(conn)
The table is created if it doesn't exist.
rws_read_meta(conn)
rws_read_meta(conn)
conn |
A SQLiteConnection to a database. |
A data frame of the meta table
conn <- rws_connect() rws_read_meta(conn) rws_write(rws_data, exists = FALSE, conn = conn) rws_read_meta(conn) rws_disconnect(conn)
conn <- rws_connect() rws_read_meta(conn) rws_write(rws_data, exists = FALSE, conn = conn) rws_read_meta(conn) rws_disconnect(conn)
Read a Table from a SQLite Database
rws_read_table(x, meta = TRUE, conn)
rws_read_table(x, meta = TRUE, conn)
x |
A string of the table name. |
meta |
A flag specifying whether to preserve meta data. |
conn |
A SQLiteConnection to a database. |
A data frame of the table.
conn <- rws_connect() rws_write(rws_data, exists = FALSE, conn = conn) rws_write(rws_data[c("date", "ordered")], x_name = "data2", exists = FALSE, conn = conn ) rws_read_table("data2", conn = conn) rws_disconnect(conn)
conn <- rws_connect() rws_write(rws_data, exists = FALSE, conn = conn) rws_write(rws_data[c("date", "ordered")], x_name = "data2", exists = FALSE, conn = conn ) rws_read_table("data2", conn = conn) rws_disconnect(conn)
Read Tables from a SQLite Database
## S3 method for class 'character' rws_read(x, meta = TRUE, conn, ...)
## S3 method for class 'character' rws_read(x, meta = TRUE, conn, ...)
x |
A character vector of table names. |
meta |
A flag specifying whether to preserve meta data. |
conn |
A SQLiteConnection to a database. |
... |
Not used. |
A named list of the data frames.
Other rws_read:
rws_describe_meta.data.frame()
,
rws_read()
,
rws_read.SQLiteConnection()
conn <- rws_connect() rws_write(rws_data, exists = FALSE, conn = conn) rws_write(rws_data[c("date", "ordered")], x_name = "data2", exists = FALSE, conn = conn ) rws_read(c("rws_data", "data2"), conn = conn) rws_disconnect(conn)
conn <- rws_connect() rws_write(rws_data, exists = FALSE, conn = conn) rws_write(rws_data[c("date", "ordered")], x_name = "data2", exists = FALSE, conn = conn ) rws_read(c("rws_data", "data2"), conn = conn) rws_disconnect(conn)
Read All Tables from a SQLite Database
## S3 method for class 'SQLiteConnection' rws_read(x, meta = TRUE, ...)
## S3 method for class 'SQLiteConnection' rws_read(x, meta = TRUE, ...)
x |
A SQLiteConnection to a database. |
meta |
A flag specifying whether to preserve meta data. |
... |
Not used. |
A named list of the data frames.
Other rws_read:
rws_describe_meta.data.frame()
,
rws_read()
,
rws_read.character()
conn <- rws_connect() rws_write(rws_data, exists = FALSE, conn = conn) rws_write(rws_data[c("date", "ordered")], x_name = "data2", exists = FALSE, conn = conn ) rws_read(conn) rws_disconnect(conn)
conn <- rws_connect() rws_write(rws_data, exists = FALSE, conn = conn) rws_write(rws_data[c("date", "ordered")], x_name = "data2", exists = FALSE, conn = conn ) rws_read(conn) rws_disconnect(conn)
Rename SQLite Column
rws_rename_column(table_name, column_name, new_column_name, conn)
rws_rename_column(table_name, column_name, new_column_name, conn)
table_name |
A string of the name of the table. |
column_name |
A string of the column name. |
new_column_name |
A string of the new name for the column. |
conn |
A SQLiteConnection to a database. |
TRUE
Other rws_rename:
rws_drop_table()
,
rws_rename_table()
conn <- rws_connect() rws_write(data.frame(x = 1), x_name = "local", exists = FALSE, conn = conn) rws_read_table("local", conn = conn) rws_rename_column("local", "x", "Y", conn = conn) rws_read_table("local", conn = conn) rws_disconnect(conn)
conn <- rws_connect() rws_write(data.frame(x = 1), x_name = "local", exists = FALSE, conn = conn) rws_read_table("local", conn = conn) rws_rename_column("local", "x", "Y", conn = conn) rws_read_table("local", conn = conn) rws_disconnect(conn)
Rename SQLite Table
rws_rename_table(table_name, new_table_name, conn)
rws_rename_table(table_name, new_table_name, conn)
table_name |
A string of the name of the table. |
new_table_name |
A string of the new name for the table. |
conn |
A SQLiteConnection to a database. |
TRUE
Other rws_rename:
rws_drop_table()
,
rws_rename_column()
conn <- rws_connect() rws_write(rws_data, exists = FALSE, conn = conn) rws_list_tables(conn) rws_rename_table("rws_data", "tableb", conn) rws_list_tables(conn) rws_disconnect(conn)
conn <- rws_connect() rws_write(rws_data, exists = FALSE, conn = conn) rws_list_tables(conn) rws_rename_table("rws_data", "tableb", conn) rws_list_tables(conn) rws_disconnect(conn)
Write to a SQLite Database
rws_write( x, exists = TRUE, delete = FALSE, replace = FALSE, meta = TRUE, log = TRUE, commit = TRUE, strict = TRUE, x_name = substitute(x), silent = getOption("rws.silent", FALSE), conn, ... )
rws_write( x, exists = TRUE, delete = FALSE, replace = FALSE, meta = TRUE, log = TRUE, commit = TRUE, strict = TRUE, x_name = substitute(x), silent = getOption("rws.silent", FALSE), conn, ... )
x |
The object to write. |
exists |
A flag specifying whether the table(s) must already exist. |
delete |
A flag specifying whether to delete existing rows before
inserting data. If |
replace |
A flag specifying whether to replace any existing rows whose inclusion would violate unique or primary key constraints. |
meta |
A flag specifying whether to preserve meta data. |
log |
A flag specifying whether to log the table operations. |
commit |
A flag specifying whether to commit the operations (calling with commit = FALSE can be useful for checking data). |
strict |
A flag specifying whether to error if x has extraneous columns or if exists = TRUE extraneous data frames. |
x_name |
A string of the name of the object. |
silent |
A flag specifying whether to suppress messages and warnings. |
conn |
A SQLiteConnection to a database. |
... |
Not used. |
An invisible character vector of the name(s) of the table(s).
Other rws_write:
rws_write.data.frame()
,
rws_write.environment()
,
rws_write.list()
conn <- rws_connect() rws_write(rws_data, exists = FALSE, conn = conn) rws_disconnect(conn)
conn <- rws_connect() rws_write(rws_data, exists = FALSE, conn = conn) rws_disconnect(conn)
Write a Data Frame to a SQLite Database
## S3 method for class 'data.frame' rws_write( x, exists = TRUE, delete = FALSE, replace = FALSE, meta = TRUE, log = TRUE, commit = TRUE, strict = TRUE, x_name = substitute(x), silent = getOption("rws.silent", FALSE), conn, ... )
## S3 method for class 'data.frame' rws_write( x, exists = TRUE, delete = FALSE, replace = FALSE, meta = TRUE, log = TRUE, commit = TRUE, strict = TRUE, x_name = substitute(x), silent = getOption("rws.silent", FALSE), conn, ... )
x |
A data frame. |
exists |
A flag specifying whether the table(s) must already exist. |
delete |
A flag specifying whether to delete existing rows before
inserting data. If |
replace |
A flag specifying whether to replace any existing rows whose inclusion would violate unique or primary key constraints. |
meta |
A flag specifying whether to preserve meta data. |
log |
A flag specifying whether to log the table operations. |
commit |
A flag specifying whether to commit the operations (calling with commit = FALSE can be useful for checking data). |
strict |
A flag specifying whether to error if x has extraneous columns or if exists = TRUE extraneous data frames. |
x_name |
A string of the name of the object. |
silent |
A flag specifying whether to suppress messages and warnings. |
conn |
A SQLiteConnection to a database. |
... |
Not used. |
Other rws_write:
rws_write()
,
rws_write.environment()
,
rws_write.list()
conn <- rws_connect() rws_list_tables(conn) rws_write(rws_data, exists = FALSE, conn = conn) rws_write(rws_data, x_name = "moredata", exists = FALSE, conn = conn) rws_list_tables(conn) rws_disconnect(conn)
conn <- rws_connect() rws_list_tables(conn) rws_write(rws_data, exists = FALSE, conn = conn) rws_write(rws_data, x_name = "moredata", exists = FALSE, conn = conn) rws_list_tables(conn) rws_disconnect(conn)
Write the Data Frames in an Environment to a SQLite Database
## S3 method for class 'environment' rws_write( x, exists = TRUE, delete = FALSE, replace = FALSE, meta = TRUE, log = TRUE, commit = TRUE, strict = TRUE, x_name = substitute(x), silent = getOption("rws.silent", FALSE), conn, all = TRUE, unique = TRUE, ... )
## S3 method for class 'environment' rws_write( x, exists = TRUE, delete = FALSE, replace = FALSE, meta = TRUE, log = TRUE, commit = TRUE, strict = TRUE, x_name = substitute(x), silent = getOption("rws.silent", FALSE), conn, all = TRUE, unique = TRUE, ... )
x |
An environment. |
exists |
A flag specifying whether the table(s) must already exist. |
delete |
A flag specifying whether to delete existing rows before
inserting data. If |
replace |
A flag specifying whether to replace any existing rows whose inclusion would violate unique or primary key constraints. |
meta |
A flag specifying whether to preserve meta data. |
log |
A flag specifying whether to log the table operations. |
commit |
A flag specifying whether to commit the operations (calling with commit = FALSE can be useful for checking data). |
strict |
A flag specifying whether to error if x has extraneous columns or if exists = TRUE extraneous data frames. |
x_name |
A string of the name of the object. |
silent |
A flag specifying whether to suppress messages and warnings. |
conn |
A SQLiteConnection to a database. |
all |
A flag specifying whether all the existing tables in the data base must be represented. |
unique |
A flag specifying whether each table must represented by no more than one data frame. |
... |
Not used. |
Other rws_write:
rws_write()
,
rws_write.data.frame()
,
rws_write.list()
conn <- rws_connect() rws_list_tables(conn) atable <- readwritesqlite::rws_data another_table <- readwritesqlite::rws_data not_atable <- 1L rws_write(environment(), exists = FALSE, conn = conn) rws_list_tables(conn) rws_disconnect(conn)
conn <- rws_connect() rws_list_tables(conn) atable <- readwritesqlite::rws_data another_table <- readwritesqlite::rws_data not_atable <- 1L rws_write(environment(), exists = FALSE, conn = conn) rws_list_tables(conn) rws_disconnect(conn)
Write a Named List of Data Frames to a SQLite Database
## S3 method for class 'list' rws_write( x, exists = TRUE, delete = FALSE, replace = FALSE, meta = TRUE, log = TRUE, commit = TRUE, strict = TRUE, x_name = substitute(x), silent = getOption("rws.silent", FALSE), conn, all = TRUE, unique = TRUE, ... )
## S3 method for class 'list' rws_write( x, exists = TRUE, delete = FALSE, replace = FALSE, meta = TRUE, log = TRUE, commit = TRUE, strict = TRUE, x_name = substitute(x), silent = getOption("rws.silent", FALSE), conn, all = TRUE, unique = TRUE, ... )
x |
A named list of data frames. |
exists |
A flag specifying whether the table(s) must already exist. |
delete |
A flag specifying whether to delete existing rows before
inserting data. If |
replace |
A flag specifying whether to replace any existing rows whose inclusion would violate unique or primary key constraints. |
meta |
A flag specifying whether to preserve meta data. |
log |
A flag specifying whether to log the table operations. |
commit |
A flag specifying whether to commit the operations (calling with commit = FALSE can be useful for checking data). |
strict |
A flag specifying whether to error if x has extraneous columns or if exists = TRUE extraneous data frames. |
x_name |
A string of the name of the object. |
silent |
A flag specifying whether to suppress messages and warnings. |
conn |
A SQLiteConnection to a database. |
all |
A flag specifying whether all the existing tables in the data base must be represented. |
unique |
A flag specifying whether each table must represented by no more than one data frame. |
... |
Not used. |
Other rws_write:
rws_write()
,
rws_write.data.frame()
,
rws_write.environment()
conn <- rws_connect() rws_list_tables(conn) rws_write(list(somedata = rws_data, anothertable = rws_data), exists = FALSE, conn = conn) rws_list_tables(conn) rws_disconnect(conn)
conn <- rws_connect() rws_list_tables(conn) rws_write(list(somedata = rws_data, anothertable = rws_data), exists = FALSE, conn = conn) rws_list_tables(conn) rws_disconnect(conn)
Validate SQLite Connection
vld_sqlite_conn(x, connected = NA)
vld_sqlite_conn(x, connected = NA)
x |
The object to check. |
connected |
A logical scalar specifying whether x should be connected. |
A flag indicating whether the object was validated.
conn <- rws_connect() vld_sqlite_conn(conn) rws_disconnect(conn) vld_sqlite_conn(conn, connected = TRUE)
conn <- rws_connect() vld_sqlite_conn(conn) rws_disconnect(conn) vld_sqlite_conn(conn, connected = TRUE)