how to use replaceall function in an sqlite where clause - java

I need to do the following query in sqlite:
select * from table where field = "somekindofdata"
however, I need to first remove all the special characters from field before I do the comparison. In java, it would look something like this:
field = field.replaceAll("[\\s+\\W+]","");
is this possible ?

Related

StringUtil indexOf() equivalent postgreSQL query

I need to implement stringUtils Class indexOf() method in postgresql.
Lets say I have a table in which url is one of the column.
url : "http://paypal-info.com/home.webapps.cgi-bin-limit/webscr.cmd-login-submit"
My requirement is to find the index of the 3rd occurence of '/' in the above url and do substring and take only paypal-info.com host name in Postgresql Query
Any idea on implementing this would be grateful.
Thanks
Have you tried split_part method?
SELECT split_part('http://paypal-info.com/home.webapps.cgi-bin-limit/webscr.cmd-login-submit', '/', 3)
Result:
split_part
paypal-info.com
For other string functions try this doc:
http://www.postgresql.org/docs/9.1/static/functions-string.html
Edit: as for indexOf itself I don't know any built-in postgres solution. But using two string functions You can achieve it like this:
SELECT strpos('http://paypal-info.com/home.webapps.cgi-bin-limit/webscr.cmd-login-submit', split_part('http://paypal-info.com/home.webapps.cgi-bin-limit/webscr.cmd-login-submit', '/', 4)) - 1 as index_of;
The string functions and operators section of the manual is the equivalent of String.indexOf, e.g.
select position('/' in 'http://paypal-info.com/home.webapps.cgi-bin-limit/webscr.cmd-login-submit');
however it doesn't offer the option to get the n'th occurrence.
You're really approaching this all wrong. You should use proper URL parsing code to extract the host portion, not attempt to roll your own or use regex / splitting / string mangling.
PostgreSQL doesn't have a native URL/URI type, but its procedural languages do and it's trivial to wrap suitable functions. e.g. with PL/Python:
create language plpythonu;
create or replace function urlhost(url text) returns text
language plpythonu
immutable strict
as $$
import urlparse
return urlparse.urlparse(url).netloc
$$;
then:
regress=# select urlhost('http://paypal-info.com/home.webapps.cgi-bin-limit/webscr.cmd-login-submit');
urlhost
-----------------
paypal-info.com
(1 row)
If you'd prefer to use PL/Perl, PL/V8, or whatever, that's fine.
For best performance, you could write a simple C function and expose that as an extension.
Just replace 3 with N to get the index of the Nth '/' in a given string
SELECT length(substring('http://asd/asd', '(([^/]*/){3})')) - 1
To extract the host name from url you can use
SELECT substring('http://asd.com:234/qwe', 'http://([^:]+).*/')
Tested here: SQLFiddle

SQLite querying for exact matches

SQLite statement 'abc' LIKE 'ABCd' will return true. So my question is how to make an SQLite query that will return only exact record matches for a particular query.
The LIKE operator has two modes that can be set by a pragma. The default mode is for LIKE comparisons to be insensitive to differences of case for latin1 characters. Thus, by default, the following expression is true:
'a' LIKE 'A'
But if the case_sensitive_like pragma is enabled as follows:
PRAGMA case_sensitive_like=ON;
Then the LIKE operator pays attention to case and the example above would evaluate to false.
for exact record match you need to use = sign.
ex : abc = abc.
and if you want all records which contain abc words. You need to write
abc like '%abc%' in your sqllite query.

How can I use JPA to search for a substring on a field?

My application uses JPA to access the backend database.
I have a Java class mapped to a table. The class has a string field (called status) that consists of a series of "0"s and "1"s. I need to select a few records based on the field's second character. Here is what I can do without using JPA (I am using MS SQLServer).
SELECT * FROM Machines where SUBSTRING(status, 2, 1) = '1'
How can I do it using JPA?
There is a SUBSTRING function in JPA:
http://download.oracle.com/otn-pub/jcp/persistence-2.0-fr-eval-oth-JSpec/persistence-2_0-final-spec.pdf
4.6.17.2.1 "String functions"
(...)
SUBSTRING(string_primary, simple_arithmetic_expression [, simple_arithmetic_expression])
(...)
The second and third arguments of the SUBSTRING function denote the starting position and
length of the substring to be returned. These arguments are integers.
The third argument is optional. If it is not specified, the substring
from the start position to the end of the string is returned. The
first position of a string is denoted by 1. The SUBSTRING function
returns a string.
JPQL has the SUBSTRING(..) function as well: see here. So it will be the same as in the native query.
When JPA doesn't support some function that you need, you can make a native query and map the result to a pojo.
JPQL has a substring function which can be used to trim entities. Make sure you use entity names in your query and also note that the substring function is not zero indexed.
SELECT * FROM Machines m where SUBSTRING(m.status, 2, 1) = '1'

Android Single Quote In SQL With a LIKE StateMent

I am having problem querying single quote while using the sql LIKE statement
this is my SQL query for searching the MUSIC file in the SD CARD.
final Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
final String[] cursor_cols = {
MediaStore.Audio.Media.TITLE
};
where = MediaStore.Audio.Media.TITLE + " like ('%"+SomeSongTitle+"%')";
cursor = getContentResolver().query(uri, cursor_cols, where, null, null);
SomeSongTitle is some arbitrary input text that the a user input.
My Question is why when SomeSongTitle contains a single Quote(for example SomeSongTitle=don't), it crashes.
And How to fix it?
thankz for reading and hope to hear some solution from you guys =D. hehe
If you don't want to do String substitution you can use SQLiteDatabase.rawQuery to get your Cursor object. And then do something like:
String query = "select * from your_table_name where" + MediaStore.Audio.Media.TITLE + " like ('%?%')";
cursor = yourDB.rawQuery(query, new String[] {SomeSongTitle});
That should get around the quoting issue.
To fix it you need to replace the single quote with two single quotes. Try using something like...
SomeSongTitle = SomeSongTitle.replace("'", "''");
If you use bindings (?) for the argument(s) in the where clause, then you do not need and should not use any single quotes because the binding already takes care of that.
In particular, the second argument in a binding is an array of strings,
String[], providing one String for each ?. In the binding process, each of those Strings is treated by sql as if it has single quotes around it. Binding creates a compiled sql statement with variable substitution, so it is efficient to write your sql as a fixed String and binding rather than make a different statement each call.
You'll need to escape the single quote. There are much more sophisticated methods to do this, but an easy way to start is to simply to a find and replace in order to add a slash (\) before the quote mark so that it looks like this: (\').
You can read more about it SQL Injection. Specifically, look at the section on Mitigation.
Android's database API sits on top of sqlite. In its FAQ, you can see that to "escape" a single quote, you just use two single quotes. See here.

Exact match with sql like and the bind

I have a bind in the SQL query
SELECT * FROM users WHERE name LIKE '%?%'
the bind set the ?.
Now, if i want to search with like method everything work but if, without change the sql, i want to search the exact match i dont now how to do.
I tried some regexp int the textbox es:
_jon \jon\ [jon] and some others but nothing work properly.
Any ideas?
Change your query to
select * from users where name like '?'
If you want to do a wildcard match, put the wildcards as part of the string that you're binding to the variable. If you don't want to do a wildcard match, then don't.
Note that like and = have the same performance except when your wildcard character is first in the string (for example, '%bob') as in that case the query optimizer can't use indexes as well to find the row(s) that you're looking for.
you can't search an exact match if the sql contains % symbols, as they are wildcards. you'll need to change the sql to
select * from users where name = '?'
for an exact match
(you can also use select * from users where name like '?' but that's more inefficient)
What is keeping you from changing the SQL?
The Like condition is for 'similar' matches, while the '=' is for exact matches.

Categories