Android - sqlite in clause using values from array - android

I want to execute a sqlite query:
select * from table_name where id in (23,343,33,55,43);
The values in the in clause need to be taken from an array of strings:
String values[] = {"23","343","33","55","43"}
How can I achieve that?

I believe a simple toString() will mostly do the trick:
String values[] = {"23","343","33","55","43"};
String inClause = values.toString();
//at this point inClause will look like "[23,343,33,55,43]"
//replace the brackets with parentheses
inClause = inClause.replace("[","(");
inClause = inClause.replace("]",")");
//now inClause will look like "(23,343,33,55,43)" so use it to construct your SELECT
String select = "select * from table_name where id in " + inClause;

Related

sqlite query to search database with multiple keywords

I used a code similar to the following code to search the database person name :
cursor.query(select column from Table where column_person_Name="John");
But now I want to search the names of a few people together,How to do it?
The way to do that using SQlite would be using IN:
SELECT * FROM persons WHERE name IN ("John", "Doe");
In Java, having an array of Strings, you could do it like that:
final String[] namesArray = {"John", "Doe"};
// Pad the Strings with quotes: John -> 'John'
for (int i = 0; i < namesArray.length; i++) {
namesArray[i] = "'" + namesArray[i] + "'";
}
// Join the Strings: 'John', 'Doe'
final String names = TextUtils.join(",", namesArray);
final String query = "SELECT * FROM persons WHERE name IN ("+names+")";
This results in:
SELECT * FROM persons WHERE name IN ('John','Doe')
You can use IN condition
SELECT *
FROM Table
WHERE column_person_Name IN ('Name1', 'Name2', 'Name3');
By using the OR operator;
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;
here an example;
cursor.rawQuery("select column from Table where column_person_Name='John' OR column_person_Name='Bob');
Other solution for your problem can be, use like Keyword, where you get multiple results based on the search.
Like is used across application partial known data.
SELECT
column_list
FROM
table_name
WHERE
column_1 LIKE %yoursearch_word%;
Room supports Collections as a query argument.
Query in Room is like:
#Query("SELECT first_name, last_name FROM user WHERE region IN (:regions)")
public List<NameTuple> loadUsersFromRegions(List<String> regions);
Check official guides.

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 || '%' ...

How to write query if I have to get output using two values from the same columns

I want to get values from table Noon for the columns category=LD for age=1.5 and 4. I have written the following query but there is something wrong.What should i add between age = '1.5' '4'(AND,+ or something else) or some whole new query??
String query = "SELECT * FROM Noon WHERE category = 'LD' AND age = '1.5' '4' " ;
Your query should be like:
String query = "SELECT * FROM Noon WHERE category = 'LD' AND age BETWEEN '1.5' AND '4'";
Your query should be like this:
String query = "SELECT * FROM Noon WHERE category = 'LD' AND age in
('1.5','4' )"
;
Or try this:
String query = "SELECT * FROM Noon WHERE category = 'LD' AND (age=
'1.5' OR age='4' )" ;
Both should work.
please try and accept the answer if successful.

updating sqlite database using a String not working

This works but i need to be able to change the value of the product_name
String query = "UPDATE UserTable SET _quantity=_quantity - 1 WHERE product_name ='Hammer'";
myDatabase.execSQL(query);
This does not work and I can't see why.
String test = "Hammer";
String query = "UPDATE UserTable SET _quantity=_quantity - 1 WHERE product_name ='%"+test+ "%'";
myDatabase.execSQL(query);
Try this code
i think it will work
ContentValues data = new ContentValues();
data.put("Column Name", column_value);
i.e,
data.put("_quantity", 3);
myDatabase.update(TABLE_NAME ,data,"product_name = ?",new String[]{test});
To match a pattern you should use the LIKE statement:
String test = "Hammer";
String query = "UPDATE UserTable SET _quantity=_quantity - 1 WHERE product_name LIKE '%"+test+ "%'";
myDatabase.execSQL(query);
You need to remove % symbols from your query. This symbols are used to match the strings values if you want to match anything like what he wants %mer% would match hammer
Try as below:
String query = "UPDATE UserTable SET _quantity=_quantity - 1 WHERE product_name ='"+test+ "'";

Update query in android

I am trying to update the database on the basis of incoming parameter but it is not updated.
i am using the following code:
public static void markFavoriteStation(String station, boolean favorite){
Log.d(AppConstants.TAG,"StationListDBIfc: +markFavoriteStation");
String Query = null;
mDb = bartDb.getWritableDatabase();
Query = "update stationlistTable set favorite ='1' where namewithabbr = '+station'";
mDb.rawQuery(Query, null);
Log.d(AppConstants.TAG,"StationListDBIfc: -markFavoriteStation");
}
I think you might have a malformed String definition. You should end the String before concatenating the "station" variable to it, like so:
Query = "update stationlistTable set favorite ='1' where namewithabbr = '" + station + "'";
I can't see any errors. I guess the SQL query has errors or the namewithabbr column doesn't contain what you expect. You should test it in the sqlite3 app.

Categories

Resources