1月 012010
 
################################################
# EXPORT DATA FRAME INTO SQLITE DATABASE       #
# USING SQLDF PACKAGE(code.google.com/p/sqldf) #
################################################

library(sqldf)
library(datasets)
data(CO2)

### SET WORKING DIRECTORY ###
setwd('d:\\r')

### TEST IF THE FILE EXISTS ###
if(file.exists('test.db')) file.remove('test.db')

### CREATE A NEW EMPTY SQLITE DATABASE ###
sqldf("attach 'test.db' as new")

### CREATE A NEW TABLE FROM DATA FRAME ###
sqldf("create table tblco as select * from CO2", dbname = 'test.db')

### SHOW THE DATA IN SQLITE DATABASE ###
sqldf("select * from tblco limit 5", dbname = 'test.db')
#  Plant   Type  Treatment conc uptake
#1   Qn1 Quebec nonchilled   95   16.0
#2   Qn1 Quebec nonchilled  175   30.4
#3   Qn1 Quebec nonchilled  250   34.8
#4   Qn1 Quebec nonchilled  350   37.2
#5   Qn1 Quebec nonchilled  500   35.3
 Posted by at 1:26 下午

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)