[SPAM Detected: 85%] Re: [R] Insert a recorde into a table using SQL

From: ronggui <ronggui.huang_at_gmail.com>
Date: Mon, 12 May 2008 19:23:10 +0800

It works. Thanks very much.

Best

On Mon, May 12, 2008 at 7:02 PM, Gabor Grothendieck <ggrothendieck_at_gmail.com> wrote:
> Create an encoding function which replaces single quotes with
> two single quotes:
>
> # first string is a single character consisting of single quote
> # second string is two characters consisting of two single quotes
> enc <- function(x) gsub("'", "''", x)
> dbGetQuery(con,sprintf("insert into dd (txt) values ('%s')", enc(dd[2,1])))
>
>
> On Mon, May 12, 2008 at 1:54 AM, ronggui <ronggui.huang_at_gmail.com> wrote:
>> 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.
>>
>> ______________________________________________
>> R-help_at_r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>

-- 
HUANG Ronggui, Wincent
Bachelor of Social Work, Fudan University, China
Master of sociology, Fudan University, China
Ph.D. Candidate, CityU of HK,
http://www.cityu.edu.hk/sa/psa_web2006/students/rdegree/huangronggui.html

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Received on Mon 12 May 2008 - 11:31:30 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 12 May 2008 - 12:30:38 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive