Skip to content

importing data to SQLite database with sqldf

2 messages · Satoshi Takahama, Gabor Grothendieck

#
Thanks yet another time, Gabor -
I think I am slowly understanding - particularly I was confused by persistence of connections.

So starting with some parts of your example 12, 

##
gc()
rm(list=ls())
unlink("mydb")
sqldf("attach 'mydb' as new") 
irishead <- file("irishead.dat")
iristail <- file("iristail.dat")

If I just wanted to merge the two files within SQL and return some part of the result, I would do

sqldf('select count(*) from (select * from irishead 
union 
select * from iristail)',dbname="mydb")

and the tables exist in mydb only for the duration of the computation
NULL
(but why is the size of mydb > 0 afterwards, if it contains no tables...?)

...is the above the same as 
sqldf('select count(*) from (select * from irishead 
union 
select * from iristail)',dbname=tempfile())

except that I don't create 'mydb'?

If I wanted to save the merged table (for use in a later session):

sqldf('create table fulliris as select * from irishead 
union 
select * from iristail',dbname="mydb")
[1] fulltable
Levels: fulltable

If I want copies of all three tables,
sqldf(dbname="mydb")
sqldf('create table fulltable as select * from irishead 
union 
select * from iristail')
sqldf()
[1] irishead  iristail  fulltable
Levels: fulltable irishead iristail

? ...I'll try to go figure a few more things out in the in the meantime (like using sep="\t" ?) and using connections with sqldf().

But thanks for the help!

Stephen

----- Original Message ----
From: Gabor Grothendieck <ggrothendieck at gmail.com>
To: Stephen Tucker <brown_emu at yahoo.com>
Cc: R-help <r-help at stat.math.ethz.ch>
Sent: Friday, February 20, 2009 5:22:09 AM
Subject: Re: [R] importing data to SQLite database with sqldf

Have just added an example 12 on the home page:

http://sqldf.googlecode.com

that shows an example.  Note use of notation
main.mytable to refer to an existing table in the
main database (as opposed to a data frame in R).
On Thu, Feb 19, 2009 at 11:55 PM, Stephen Tucker <brown_emu at yahoo.com> wrote:
#
WARNING!!!

Not a good idea to post code that deletes every object
in your workspace!   Other people may blindly copy
and paste your code.

I've added some verbiage to Example 12 on the home
page:
http://sqldf.googlecode.com
that hopefully clarifies it a bit.
On Sat, Feb 21, 2009 at 2:36 AM, Stephen Tucker <brown_emu at yahoo.com> wrote:
... deleted code which clears workspace ...
Yes.  The third possibility is to omit dbname= entirely and then it uses
a temporary "in memory" database.