Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing FIles with Extension .sqlite into R

Tags:

I have a SQLite database exported (as a sqlite format 3 file?) from Scraperwiki with the .sqlite file extension/file suffix.

How do I import it into R, presumably mapping the original database tables into separate data frames?

like image 263
psychemedia Avatar asked Mar 21 '12 10:03

psychemedia


People also ask

How do I open a .SQLite file?

Open a command prompt (cmd.exe) and 'cd' to the folder location of the SQL_SAFI. sqlite database file. run the command 'sqlite3' This should open the SQLite shell and present a screen similar to that below.

What is the R package of SQLite?

RSQLite: SQLite Interface for R Embeds the SQLite database engine in R and provides an interface compliant with the DBI package. The source for the SQLite engine and for various extensions in a recent version is included.


2 Answers

You could use the RSQLite package.

Some example code to store the whole data in data.frames:

library("RSQLite")  ## connect to db con <- dbConnect(drv=RSQLite::SQLite(), dbname="YOURSQLITEFILE")  ## list all tables tables <- dbListTables(con)  ## exclude sqlite_sequence (contains table information) tables <- tables[tables != "sqlite_sequence"]  lDataFrames <- vector("list", length=length(tables))  ## create a data.frame for each table for (i in seq(along=tables)) {   lDataFrames[[i]] <- dbGetQuery(conn=con, statement=paste("SELECT * FROM '", tables[[i]], "'", sep="")) } 
like image 162
sgibb Avatar answered Sep 20 '22 05:09

sgibb


To anyone else that comes across this post, a nice way to do the loop from the top answer using the purr library is:

lDataFrames <- map(tables, ~{   dbGetQuery(conn=con, statement=paste("SELECT * FROM '", .x, "'", sep="")) }) 

Also means you don't have to do:

lDataFrames <- vector("list", length=length(tables)) 
like image 40
primaj Avatar answered Sep 18 '22 05:09

primaj