[R] Insert a recorde into a table using SQL

ronggui ronggui.huang at gmail.com
Mon May 12 07:54:28 CEST 2008


Dear list,

I want to insert a recorde into a SQLite table by dbGetQuery(), but
there is a problem when the value contains quotation mark.

> dd<-data.frame(txt=c("having both ' and \" in character.","OK"))
> library(RSQLite)
Loading required package: DBI
> con<-dbConnect(dbDriver("SQLite"),":memory:")
> dbWriteTable(con,"dd",dd,over=T)
[1] TRUE
> dbGetQuery(con,sprintf("insert into dd (txt) values (\"%s\")",dd[2,1]))
NULL
> dbGetQuery(con,sprintf("insert into dd (txt) values (\"%s\")",dd[1,1]))
Error in sqliteExecStatement(con, statement, bind.data) :
  RS-DBI driver: (error in statement: unrecognized token: "")")

How can I insert a (key, value) pair into a table by dbGetQuery?  Thanks.

-- 
HUANG Ronggui, Wincent
Bachelor of Social Work, Fudan University, China
Master of sociology, Fudan University, China
Ph.D. Candidate, CityU of HK.



More information about the R-help mailing list