Connect

library(DBI)
library(RSQLite)
portalDB <- "portal_mammals.sqlite"
conn <- dbConnect(SQLite(), portalDB)

Checkout the database

dbListTables(conn)
dbListFields(conn, "plots")
dbListFields(conn, "surveys")

Run queries

query <- "SELECT genus, species, COUNT(*)
          FROM surveys JOIN species
		  ON surveys.species_ID = species.species_ID
		  GROUP BY genus, species"
species_counts <- dbGetQuery(conn, query)

Exercise 1

Write new information to database

dbWriteTable(conn, "SpeciesCounts", species_counts)
viewquery <- paste("CREATE VIEW SpeciesCounts AS", query)
dbSendQuery(conn, viewquery)

Exercise 3