Android SQLite wildcard to select character pattern - android

I am using an SQLite database in Android. I want to query the database using a query like
SELECT * FROM Persons WHERE City LIKE '%nes%'
which would return the city Sandnes based on the character pattern 'nes' matching, as in: http://www.w3schools.com/sql/sql_wildcards.asp.
However, when I try to put this into android's
query (SQLiteDatabase db, String[] projectionIn, String selection, String[] selectionArgs, String groupBy, String having, String sortOrder)
method, using
selection = KEY_CODE + " LIKE ?"; and selectionArgs = new String[] {"'%" + query + "%'"};,
I get no results returned.
Can I do this in the android query? If so, how should I write it? And if not, is there an alternative way?
Thanks in advance.

You're building a "likeable" parameter that looks like this:
'%do you like me%'
The apostrophes should not be there: they belong to literal syntax, not to a pattern itself:
%now you like me%
Use
selectionArgs = new String[] {"%" + query + "%"};

Related

Android SQLite query cursor moveToFirst

I'm making an Android App with a sqlite db, which has a query method like this:
String[] projection = {COLUMN_NAME_ID};
String selection = COLUMN_NAME_TEAM + " = ?";
String[] selectionArgs = {teamId.toString()};
String sortOrder = COLUMN_NAME_NAME + " ASC";
Cursor c = db.query(TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder);
if (c.moveToFirst()){
//...
}
as suggested here. The point is to implement this query:
SELECT PLAYER_ID FROM PLAYERS WHERE TEAM_ID = ? ORDER BY NAME ASC;
According to the SQLiteDatabase.query documentation, this is a valid call to query(), and so far I've been doing it successfully with all my other queries in this App.
For some reason, in this query (and only in this) the App freezes in c.moveToFirst().
Some answers to similar questions suggest that it might be a performance issue. I don't think this is the case, as my table has only 6 rows and the query is quite simple.
Any ideas?
Thanks

Android dynamic query sql using like

I would make a query using like , using the % and a dynamic string, but i can't do it correctly.
I have a:
public Cursor query(String s)
{
String whereClause=COL_ATTRIBUTI + " like '%?%'";
String[] whereArgs=new String[] {s};
return getWritableDatabase().query(TABELLA_RICETTE,null,whereClause,whereArgs,null,null,null);
}
COL_ATTRIBUTI is the name of a column and TABELLA_RICETTE the name of the table.
I would like to obtain all the thing that have s somewhere, so i use the %. but my app crash. What is the correct syntax?
I think you need to put the % wildcards in the argument if you are using the ? placeholders:
String whereClause = COL_ATTRIBUTI + " like '?'";
String[] whereArgs = new String[] {"%" + s + "%"};

Using like with where clause for deleting in android

I am developing an android app, where I wan to delete a row based on a where clause accompanied with a like clause too. Below is the code what I have tried,
String where = "numberp Like '%''"+phonetounbold+"'";
String[] whereArgs = null;
delcount = db.delete("NUMBERLIST", where, whereArgs);
In the above table I want to delete a row from NUMBERLIST table , which contains phonetounbold like string from numberp column. But the above query is not working. Where am I going wrong with the syntax. Please help. Thanks!
Try to change the "where" clause to
String where = "numberp LIKE '%"+phonetounbold+"'";
If you are developing using the raw SQLiteDatabase interface, not the content provider, you can use execSQL method directly:
database.execSQL("delete from numberlist where numberp like '%xyz%'")
Try this
String where = "numberp Like '%" + phonetounbold + "'";

Android sqlite query for string

Cursor cursor = db.rawQuery("SELECT id,lastname FROM people WHERE lastname='John Kenedy'; ",null);
Is that correct usage of comparing String ? if no, How can I compare String values in the database?
For better performance, you should use rawQuery method with selectionArgs which is faster and more secure against adding directly data to statement. So try it like this
Cursor cursor = db.rawQuery("SELECT id,lastname FROM people WHERE lastname = ?; ", new String[] {"John Kenedy"});

Android sql query using WHERE clause

Dear Stack Overflow Community,
I have a question regarding how to incorporate a WHERE clause when querying a sql database in Android. My goal is to return specific records from my database where the date picked by a datepicker matches the date stored.
Here is my code:
private static String datePickerDate = "3/29/2011";
private static String[] FROM = { _ID, NAME, PRICE, DATE, TIME };
private static String ORDER_BY = TIME + " DESC ";
private static String WHERE = DATE + "is like " + datePickerDate;
private Cursor getEvents(){
// Perform a managed Query. The Activity will handle closing
// and re-querying the cursor when needed
SQLiteDatabase db = events.getReadableDatabase();
Cursor cursor = db.query(TABLE_NAME, FROM, null, null, null, null, ORDER_BY);
startManagingCursor(cursor);
return cursor;
}
My questions are thus:
Where does my "WHERE" statement go?
And is my "WHERE" statement correct?
The documentation I found for db.query doesn't specify if a "WHERE" clause can be used, it just specifies that a "HAVING" clause is possible, but I don't quite think that's what I'm wanting.
db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy)
The selection represents your WHERE clause.
From the doc
selection A filter declaring which
rows to return, formatted as an SQL
WHERE clause (excluding the WHERE
itself). Passing null will return all
rows for the given table.
So you can try this (untested):
private static String WHERE = "DATE like ?";
db.query(table, columns, WHERE , new String[] { datePickerDate }, groupBy, having, orderBy)
If you want to use the WHERE clause, I would suggest using the raw query function in the SQLiteDatabase class, shown here.
This way, you can have the raw query typed out (or in segments) as if you were doing it naturally with SQL.
EDIT: On a side note, the "selection" parameter of the query() function corresponds to the WHERE clause, as noted here
About where you're "WHERE" goes - look here
And regarding the second part, I think you miss the qouts before and after the value, it should be .. like '3/29/2011' and not .. is like 3/29/2011

Categories

Resources