Cannot use Like statement in Android SQLite with argument and % character - android

Caused by: java.lang.IllegalArgumentException: Cannot bind argument at index 1 because the index is out of range. The statement
has 0 parameters.
Android doesn't recognise ? character in ?%
I try to achieve this:
String selectQuery = "SELECT * FROM config WHERE Directory Like '?%';";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, new String[] {parent});
I have searched for a solution but couldn't find anything, I think it's pretty particullary. I am trying to do it like this because I want to make the query work with Strings that contains character '.
Maybe someone who had the same problem can answer me how can I solve this.

Use || to concat parameter and wildcard:
SELECT * FROM config WHERE Directory Like ? || '%';

SELECT * FROM config WHERE Directory Like '?' || '%';
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, new String[] {parent});
using || solves this issue

you can put what condition you have into a String variable and then pass it to your query like this--> String Condition="'?";
SELECT * FROM config WHERE Directory Like '"+Condition+"'

Related

Check if column string in database is a substring of a query in sqlite

the current Sqlite syntax can check if a query is a substring of a column string.
and I can do a hack to add a % behind the ? described in https://stackoverflow.com/a/5752671/908821
final String whereClause = DbHelper.COLUMN_URL + " LIKE ? " ;
is there a syntax where i can do the opposite? I want to check if the column string is a substring of a query.
The following code does not seem to work.
final String whereClause = "? LIKE " + DbHelper.COLUMN_URL ;
The following are the rest of my codes:
String[] whereArg = {url};
ContentValues values = new ContentValues();
values.put(DbHelper.COLUMN_LAST_URL, lastUrl);
database.updateWithOnConflict(DbHelper.TABLE_BOOKMARK,
values,
whereClause,
whereArg,
SQLiteDatabase.CONFLICT_IGNORE);
I am trying to update "LastUrl" column if the base url is a subString of a query.
some examples that i like to catch
Base Url (in database) Query
-----------------------------------------------------------------
http://example.com/path http://example.com/path/path2
http://example.com/path?id=0 http://example.com/path?id=0&x=xx
The LIKE operator checks whether the value on the left matches the pattern on the right. So just put the value on the left, and the pattern on the right.
If the pattern needs a % that is not in the database, you have to append it:
SELECT ... WHERE ? LIKE LastUrl || '%' ...

Sqlite Select Query with rawQuery at Android

I know it is a basic question, but i can not find problem.
I try to get some columns with rawQuery.
SQLiteDatabase db=database.getReadableDatabase();
try
{
String sql="SELECT * FROM CAR WHERE name = ";
Cursor crs = db.rawQuery(sql+"5P", null);
}
I always get the same exception.
android.database.sqlite.SQLiteException: **unrecognized token**: "5P": , while compiling: SELECT * FROM CAR WHERE name = 5P
I have 5P at CAR table, i am sure because i used it other queries.
You either need to quote that value, or better yet, use positional parameters:
String[] args={"5P"};
Cursor crs=db.rawQuery("SELECT * FROM CAR WHERE name = ?", args);
The advantage of using positional parameters is that SQLite will handle quoting the string, escaping any embedded quotes, etc.

What is the correct syntax for SELECT statement and WHERE clause?

I am trying to explore android and I just started using SQLite database. I'm wondering on what is the right syntax for selecting a single row from a table, where the row I want to select is from the value entered from a user using editText. Thanks in advance.
I'm going to disagree with both of the answers above. What if the user enters this query:
Bobby Tables'; drop table yourTable;
See: http://xkcd.com/327/
I believe you should do this instead:
String query = "select * from TABLE_NAME WHERE column_name=?";
String[] selection = new String[1];
selection[0] = users_entered_value;
Cursor c = db.rawQuery(query, selection);
ETA: Actually, the more I think about it, the more I think you're going in the wrong direction. If your app depends on a database query returning exactly one unique match to an arbitrary string entered by the user, it's probably going to be broken a great deal of the time.
What you should probably do is something like this:
String query = "select * from TABLE_NAME WHERE column_name LIKE ?";
String[] selection = new String[1];
selection[0] = "%" + users_entered_value + "%";
Cursor c = db.rawQuery(query, selection);
and then iterate through the results and pick a "best" match according to your own criteria.
Also, you should create the table with case-insensitive matching for the column(s) you're going to be searching.
SQLiteDatabase db;
db.rawQuery("select * from yourTable where your_column_name = 'users_entered_value' limit 1", null);
SQLiteDatabase db;
// make connection to your database ;
Cursor c = null ;
String SQL = "select * from TABLE_NAME where column_name='VALUE'";
c = db.rawQuery(SQL);
c contains your result array of query you fired.
You can retrieve values using loop.

Database retreival in android

I am retrieving data from database using select query.My requirement is i have to sum the (total) column where group=household and category=Income.I have 1 entry in database which satisfy this condition.But it returns always 0.Please help me.
My query is:
long sum=0;
Cursor cursor1 = db.rawQuery(
"SELECT SUM("+(KEY_TOTAL)+") FROM incomexpense WHERE groups='Household' & category='Income'",null);
if(cursor1.moveToFirst())
{
sum = cursor1.getLong(0);
}
cursor1.close();
String housetotal=String.valueOf((long)sum);
System.out.println("house="+housetotal);
Try replacing the & with AND in the query.
I guess the query might not have worked because you have written '&' instead of 'and'

android sqLite prob with where clause

I am building a cursor for a select query. The WHERE section lookup value refers to a variable which is a path so it has several full stops in it.
The query doesnt like this and errors , the error says a col doesn't exist named with a name which is the lookup value i.e the path with stops in it. If I use the sqlEscape util on the variable it doesnt cause an error but the lookup fails.
Any suggestions please?
Without a bit of your code it is hard to say but I'm guessing you are putting your where clause together like this:
String value = "some.thing.or.other";
String where = "FIELD = " + value;
Try building a parameterized where clause instead
String value = "some.thing.or.other";
String where = "FIELD = ?";
SQLiteDatabase db = fDbOpener.getReadableDatabase();
Cursor results = db.query(TABLE_NAME, COLUMNS_LIST, where, new String[] { value }, null, null, null);
When you run the query method now, SQLite will substitute your value into the where clause specifically as a value - it won't try to interpret the value in any way so it can't mistake the full stops for syntax.

Categories

Resources