Together, SQLite() and dbConnect() allow you to connect to a SQLite database file. See DBI::dbSendQuery() for how to issue queries and receive results.
...: In previous versions, SQLite() took arguments. These have now all been moved to DBI::dbConnect(), and any arguments here will be ignored with a warning.
drv, conn: An objected generated by SQLite(), or an existing SQLiteConnection. If an connection, the connection will be cloned.
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:
"" will create a temporary on-disk database. The file will be deleted when the connection is closed.
":memory:" or "file::memory:" will create a temporary in-memory database.
loadable.extensions: When TRUE (default) SQLite3 loadable extensions are enabled. Setting this value to FALSE
prevents extensions from being loaded.
default.extensions: When TRUE (default) the initExtension()
function will be called on the new connection.Setting this value to FALSE
requires calling initExtension() manually.
cache_size: Advanced option. A positive integer to change the maximum number of disk pages that SQLite holds in memory (SQLite's default is 2000 pages). See https://www.sqlite.org/pragma.html#pragma_cache_size
for details.
synchronous: Advanced options. Possible values for synchronous
are "off" (the default), "normal", or "full". Users have reported significant speed ups using sychronous = "off", and the SQLite documentation itself implies considerable improved performance at the very modest risk of database corruption in the unlikely case of the operating system (not the R application) crashing. See https://www.sqlite.org/pragma.html#pragma_synchronous for details.
flags: SQLITE_RWC: open the database in read/write mode and create the database file if it does not already exist; SQLITE_RW: open the database in read/write mode. Raise an error if the file does not already exist; SQLITE_RO: open the database in read only mode. Raise an error if the file does not already exist
vfs: Select the SQLite3 OS interface. See https://www.sqlite.org/vfs.html for details. Allowed values are "unix-posix", "unix-unix-afp", "unix-unix-flock", "unix-dotfile", and "unix-none".
bigint: The R type that 64-bit integer types should be mapped to, default is bit64::integer64 , which allows the full range of 64 bit integers.
extended_types: When TRUE columns of type DATE, DATETIME / TIMESTAMP, and TIME are mapped to corresponding R-classes, c.f. below for details. Defaults to FALSE.
Returns
SQLite() returns an object of class SQLiteDriver .
dbConnect() returns an object of class SQLiteConnection .
Details
Connections are automatically cleaned-up after they're deleted and reclaimed by the GC. You can use DBI::dbDisconnect() to terminate the connection early, but it will not actually close until all open result sets have been closed (and you'll get a warning message to this effect).
Extended Types
When parameter extended_types = TRUE date and time columns are directly mapped to corresponding R-types. How exactly depends on whether the actual value is a number or a string:
Column type
Value is numeric
Value is Text
R-class
DATE
Count of days since 1970-01-01
YMD formatted string (e.g. 2020-01-23)
Date
TIME
Count of (fractional) seconds
HMS formatted string (e.g. 12:34:56)
hms (and difftime )
DATETIME / TIMESTAMP
Count of (fractional) seconds since midnight 1970-01-01 UTC
DATE and TIME as above separated by a space
POSIXct with time zone UTC
If a value cannot be mapped an NA is returned in its place with a warning.
Examples
library(DBI)# Initialize a temporary in memory database and copy a data.frame into itcon <- dbConnect(RSQLite::SQLite(),":memory:")data(USArrests)dbWriteTable(con,"USArrests", USArrests)dbListTables(con)# Fetch all query results into a data frame:dbGetQuery(con,"SELECT * FROM USArrests")# Or do it in batchesrs <- dbSendQuery(con,"SELECT * FROM USArrests")d1 <- dbFetch(rs, n =10)# extract data in chunks of 10 rowsdbHasCompleted(rs)d2 <- dbFetch(rs, n =-1)# extract all remaining datadbHasCompleted(rs)dbClearResult(rs)# clean updbDisconnect(con)
See Also
The corresponding generic functions DBI::dbConnect() and DBI::dbDisconnect().