poissonconsulting / poissqlite

An R package to facilitate working with SQLite databases
http://www.poissonconsulting.ca/poissqlite
Other
0 stars 0 forks source link

psq_extract function #3

Closed sebdalgarno closed 7 years ago

sebdalgarno commented 7 years ago

psq_tbl_extract - function to extract tables from sqlite table into tibbles in globalenv psq_blob_extract - function to extract blobs from sqlite database and write to given path

sebdalgarno commented 7 years ago

something like:

psq_tbl_extract <- function(dbname = "output/dbs/horseshoe-ex-17.sqlite3") {
  database <- src_sqlite(dbname)
  names <- src_tbls(database)
  tables <- list(src_tbls(database))

  for (i in 1:length(names)) {
    tables[[i]] <- tbl(database, names[i])
    assign(tolower(names[i]), collect(tables[[i]], n = Inf), envir = globalenv())
  }
}
psq_blob_extract <- function(dbname, table, colname, obj) {
  conn <- DBI::dbConnect(RSQLite::SQLite(), dbname = dbname)
  retr <- DBI::dbGetQuery(conn, paste("select", colname, "from" table), collapse = " ")
  blob <- lapply(retr[,colname], 'unserialize')
  names(blob) <- obj[,colname]
}

write to dir:

for(i in 1:length(vrl)) {
  writeBin(vrl[[i]], con = paste0(outdir, names(vrl)[i]), size = NA_integer_,
           endian = .Platform$endian, useBytes = FALSE)
}