Connect to an SQLite database

Together, SQLite() and dbConnect() allow you to connect to a SQLite database file. See DBI::dbSendQuery() for how to issue queries and receive results.


# S4 method for SQLiteDriver dbConnect(drv, dbname = "", ..., loadable.extensions = TRUE, default.extensions = loadable.extensions, cache_size = NULL, synchronous = "off", flags = SQLITE_RWC, vfs = NULL, bigint = c("integer64", "integer", "numeric", "character"))

# S4 method for SQLiteConnection dbConnect(drv, ...)

# S4 method for SQLiteConnection dbDisconnect(conn, ...)


In previous versions, SQLite() took arguments. These have now all been moved to 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.


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.


When TRUE (default) SQLite3 loadable extensions are enabled. Setting this value to FALSE prevents extensions from being loaded.


When TRUE (default) the initExtension() function will be called on the new connection.Setting this value to FALSE requires calling initExtension() manually.


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 http://www.sqlite.org/pragma.html#pragma_cache_size for details.


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 http://www.sqlite.org/pragma.html#pragma_synchronous for details.


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


Select the SQLite3 OS interface. See http://www.sqlite.org/vfs.html for details. Allowed values are "unix-posix", "unix-unix-afp", "unix-unix-flock", "unix-dotfile", and "unix-none".


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.


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).


SQLite() returns an object of class '>SQLiteDriver.

dbConnect() returns an object of class '>SQLiteConnection.

See Also

The corresponding generic functions DBI::dbConnect() and DBI::dbDisconnect().

  • SQLite
  • RSQLite
  • RSQLite-package
  • dbConnect,SQLiteDriver-method
  • dbConnect,SQLiteConnection-method
  • dbDisconnect,SQLiteConnection-method
# Initialize a temporary in memory database and copy a data.frame into it
con <- dbConnect(RSQLite::SQLite(), ":memory:")
dbWriteTable(con, "USArrests", USArrests)

# Fetch all query results into a data frame:
dbGetQuery(con, "SELECT * FROM USArrests")

# Or do it in batches
rs <- dbSendQuery(con, "SELECT * FROM USArrests")
d1 <- dbFetch(rs, n = 10)      # extract data in chunks of 10 rows
d2 <- dbFetch(rs, n = -1)      # extract all remaining data

# clean up
# }
Documentation reproduced from package RSQLite, version 2.1.2, License: LGPL (>= 2)

Community examples

mark@niemannross.com at Dec 12, 2018 RSQLite v2.1.1

[Example files for LinkedIn Learning:](https://linkedin-learning.pxf.io/rweekly_rsqlite) ```r # Description: Example file for RSQLite # main idea: Use SQLite to store/buffer data. it's persistent and uses SQL # Set up SQLIte ----------------------------------------------------------- # import necessary libraries install.packages(c("DBI","RSQLite")) # SQLite support library(DBI) library(RSQLite) putSQLiteHere <- "myRsqlite.sqlite" # could also be ":memory:" mySQLiteDB <- dbConnect(RSQLite::SQLite(),putSQLiteHere) # Load data into SQLite Database ----------------------------------------------------------- data("ChickWeight") # need some data to play with dbWriteTable(conn=mySQLiteDB,name="SQLChickWeight",value=data.frame(ChickWeight)) # Conn = connection to database # name = name of table to create # value is a data.frame # Use SQL with the database --------------------------------------------- doThisSQL <- "select Chick, weight from SQLChickWeight where weight > 100" dbGetQuery(mySQLiteDB,doThisSQL) # All datasets are loaded with RSQLite ------------------------------------ sqldatasets <- RSQLite::datasetsDb() dbReadTable(sqldatasets,"ChickWeight") dbGetQuery(sqldatasets,"select Chick, weight from ChickWeight where weight > 100") # additional code support ------------------------------------------------- RSQLite::initExtension(mySQLiteDB) # adds: acos, acosh, asin, asinh, atan, atan2, atanh, atn2, ceil, cos, cosh, # cot, coth, degrees, difference, exp, floor, log, log10, pi, power, radians, # sign, sin, sinh, sqrt, square, tan, tanh, charindex, leftstr, ltrim, padc, # padl, padr, proper, replace, replicate, reverse, rightstr, rtrim, strfilter, # trim, stdev, variance, mode, median, lower_quartile, upper_quartile doThisSQL <- "select Chick, median(weight) from SQLChickWeight group by Chick order by cast(Chick as int)" dbGetQuery(mySQLiteDB,doThisSQL) dbDisconnect(mySQLiteDB) # documentation ----------------------------------------------------------- browseURL("https://cran.r-project.org/web/packages/RSQLite/RSQLite.pdf") ```