Java Reference
In-Depth Information
The solution is simple: you simply double up the apostrophe, as you have seen in the method
fixApostrophes()
in earlier chapters. Here's an example:
String fixApostrophes(String in){
int n=0;
while((n=in.indexOf("'",n))>=0){
in = in.substring(0,n)+"'"+in.substring(n);
n+=2;
}
return in;
}
This simple fix is worth implementing, as it's very annoying for an Irishman to be told he has spelled his
name incorrectly (as frequently happens to me when logging on to a Web site). It's also quite surprising
how frequently apostrophes appear in normal text (as this paragraph demonstrates).
Two other characters that require escape sequences are:
Percent (%)
Underscore (_)
These are handled by defining an escape character at the end of the query in which the characters are
used.
The escape character is defined in curly braces ({}) using the keyword
escape
, as follows:
{escape 'escape-character'}
For example, the following query finds names that begin with an underscore. It uses the backslash
character as an escape character:
SELECT name
FROM variables
WHERE Id LIKE `\_%' {escape '\'};
Subqueries
A
query
is a SQL command that uses the
SELECT
keyword to return an array of data fields from one or
more tables. A
subquery
is simply a query that is used as part of another SQL statement. Subqueries
can be nested inside any of the following types of SQL statements:
SELECT
or
SELECT...INTO
INSERT...INTO
DELETE
UPDATE
Inside another subquery