Unfortunately if your string has ' inside it, R will interpret the double quote as 'this is the end of the string', not as 'this is the character ' '. This is one time you can forget the first guideline and use the single quote, ', to define the string. Finally, there are cases where you need both ' and ' inside the string. Each pattern matching function has the same first two arguments, a character vector of strings to process and a single pattern to match. Stringr provides pattern matching functions to detect, locate, extract, match, replace, and split strings. I’ll illustrate how they work with some strings and a regular expression designed to match (US. Three types of quotes are part of the syntax of R: single and double quotation marks and the backtick (or back quote, `). In addition, backslash is used to escape.
Description
Call this method to generate a string that is suitable foruse in a query as a string literal, to make sure that yougenerate valid SQL and protect against SQL injection attacks.
Sexpr[results=rd,stage=render]{DBI:::methods_as_rd('dbQuoteString')}R Quotes In String Quartets
Usage
14.2 String basics. You can create strings with either single quotes or double quotes. Unlike other languages, there is no difference in behaviour. I recommend always using ', unless you want to create a string that contains multiple '. The syntax for raw strings is exactly the same as for normal strings with the exception of the raw string operator, the letter 'r,' which precedes the quotation marks. The 'r' can be lowercase (r) or uppercase (R) and must be placed immediately preceding the first quote mark. Print r'n' prints n and print R'n'prints n%.
Arguments
conn | A DBIConnection object, as returned by |
x | A character vector to quote as string. |
... | Other arguments passed on to methods. |
Value
dbQuoteString()
returns an object that can be coerced to character,of the same length as the input.For an empty character vector this function returns a length-0 object.
When passing the returned object again to dbQuoteString()
as x
argument, it is returned unchanged.Passing objects of class SQL should also return them unchanged.(For backends it may be most convenient to return SQL objectsto achieve this behavior, but this is not required.)
Specification
The returned expression can be used in a SELECT ...
query,and for any scalar character x
the value ofdbGetQuery(paste0('SELECT ', dbQuoteString(x)))[[1]]
must be identical to x
,even if x
containsspaces,tabs,quotes (singleor double),backticks,or newlines(in any combination)or is itself the result of a dbQuoteString()
call coerced back tocharacter (even repeatedly).If x
is NA
, the result must merely satisfy is.na()
.The strings 'NA'
or 'NULL'
are not treated specially.
R Quotes In String Banjo
NA
should be translated to an unquoted SQL NULL
,so that the query SELECT * FROM (SELECT 1) a WHERE ... IS NULL
returns one row.
Passing a numeric,integer,logical,or raw vector,or a listfor the x
argument raises an error.
See Also
Other DBIResult generics: DBIResult-class
,dbBind()
,dbClearResult()
,dbColumnInfo()
,dbFetch()
,dbGetInfo()
,dbGetRowCount()
,dbGetRowsAffected()
,dbGetStatement()
,dbHasCompleted()
,dbIsReadOnly()
,dbIsValid()
,dbQuoteIdentifier()
,dbQuoteLiteral()
,dbUnquoteIdentifier()