# more information about RSQLite package:
# https://cran.r-project.org/web/packages/RSQLite/RSQLite.pdf
# example vignettes
# https://cran.r-project.org/web/packages/RSQLite/vignettes/RSQLite.html
# if the RSQLite package is not installed, do this
# install.packages("RSQLite")
# load the package into the current R session
library(RSQLite)
# connect to the database (the database must exist)
# create database in sqlite3 console and then save with .save command
mydb <- dbConnect(RSQLite::SQLite(), "c:/temp/orders.db")
# list all the tables in the database
dbListTables(mydb)
[1] "customers"   "orders"      "salespeople"
# a simple query into a data frame
df <- dbGetQuery(mydb, 'SELECT * FROM orders LIMIT 100')
# print the first few lines of the data in df
head(df)
# load a large data set that doesn't fit into memory
rs <- dbSendQuery(mydb, 'SELECT * FROM orders')
while (!dbHasCompleted(rs)) {
  df <- dbFetch(rs, n = 10)
  print(nrow(df))
}
[1] 2
# disconnect from database and free memory and resources
df <- null
Error: object 'null' not found
# perform some calculations -- this one below is nonsense and for show only
m <- mean(df$id)
m
[1] 1.5
m2 <- mean(df[,1])
m2
[1] 1.5
LS0tDQp0aXRsZTogIlJTUUxpdGUgLSBsb2FkaW5nIGRhdGEgZnJvbSBhIGRhdGFiYXNlIg0Kb3V0cHV0OiBodG1sX25vdGVib29rDQotLS0NCmBgYHtyfQ0KIyBtb3JlIGluZm9ybWF0aW9uIGFib3V0IFJTUUxpdGUgcGFja2FnZToNCiMgaHR0cHM6Ly9jcmFuLnItcHJvamVjdC5vcmcvd2ViL3BhY2thZ2VzL1JTUUxpdGUvUlNRTGl0ZS5wZGYNCg0KIyBleGFtcGxlIHZpZ25ldHRlcw0KIyBodHRwczovL2NyYW4uci1wcm9qZWN0Lm9yZy93ZWIvcGFja2FnZXMvUlNRTGl0ZS92aWduZXR0ZXMvUlNRTGl0ZS5odG1sDQoNCiMgaWYgdGhlIFJTUUxpdGUgcGFja2FnZSBpcyBub3QgaW5zdGFsbGVkLCBkbyB0aGlzDQojIGluc3RhbGwucGFja2FnZXMoIlJTUUxpdGUiKQ0KDQojIGxvYWQgdGhlIHBhY2thZ2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