Isn't that a postgresql function? I'd have to get the string from R
into postgres in order for postgres to quote it. But I'd have to
quote the string before I'd be able to send it to postgres.
res <- dbSendQuery(paste("select quote_literal(", value, sep='"'))
that's not going to work if value contains
hello'there
because postgres will see the string as
select quote_literal('hello'there')
which is a syntax error.
Every database library that I've seen, other than R's DBI interface,
has some ability to do bind-variables, where you put a placeholder in
the query string and then separately pass an array of arguments to be
properly quoted and substituted into the query.
For example, libpq has PQexecParams.
However, just because DBI doesn't support this style of working
doesn't mean the RPostgreSQL can't support it. ROracle allows you to
use bind queries by passing a "data" parameter to dbSendQuery.
Is this omission here the result of the fact that R's developers are
statisticians writing programs for themselves, rather than software
developers that have to code defensively and check input from
untrusted users? True, my program's input will only be coming from
trusted users, but that doesn't mean that I'm comfortable opening the
floodgates for sql injection attacks.
--Ryan
On Aug 29, 1:27 pm, Joe Conway <
m...@joeconway.com> wrote:
> On 08/29/2012 10:24 AM, Ryan Hughes wrote:
>
> > On Aug 29, 12:55 pm, Ian Gow <
iand...@gmail.com> wrote:
> >> Something like this?
> >> sql <- paste("SELECT * FROM table WHERE param=", value, sep="")
> >> res <- dbSendQuery(con, sql)
>
> > That doesn't quote the value. If value contains
> > '; DELETE FROM table; --
>
> > then you lose all your data.
>
> See quote_literal()
http://www.postgresql.org/docs/9.1/interactive/functions-string.html