SQliteDatabase query construction problem in Android - android

I am trying to build a query in SQLite in Android. Query is very simple and looks like this:
SELECT
ifnull(object.icon_large, ifnull( object.image, item.IMG_DEF_COVER))
FROM
object , item
WHERE
object.id_item = 1 AND item.id_item = 1;
I've tried to use SQLiteDatabase.query(), but it can be used only for one table. Any suggestions how to combine queries or how to join them, using Android SQLiteDatabases query's syntax?
Thanks in advance!!!

#fox you can use rawquery() instead of query example:
SQLiteDatabase mydatabase;
mydatabase.rawquery("select * from a, b where a.id=b.someid", null);
or can use querybuilder's setTable() method to specify the joins example
builder.setTables(TABLEA+" LEFT JOIN "+TABLEB+" ON "+TABLEB+"."+KEY_ID+"="+TABLEA+"."+KEY_B_ID);

SELECT
ifnull(object.icon_large, ifnull(object.image, item.IMG_DEF_COVER))
FROM
object
JOIN
item
ON
object.id_item = item.id_item
WHERE
object.id_item = 1;
SELECT syntax

Related

android sqlite help to fire my own query?

hello guys i have a simple intersect query works fine for sqlite3 console on windows
But how can i fire it and retrieve results in android sqlite database.
eg.
SELECT b_name FROM bus,route,station WHERE s_name='STAITON_NAME'
AND route.b_id=bus.b_id AND route.s_id=station.s_id INTERSECT
SELECT b_name FROM bus,route,station WHERE s_name='STATION_NAME' AND route.b_id=bus.b_id AND route.s_id=station.s_id;
To fire a query, use rawQuery.
To pass two different station names, use two different parameters:
c = db.rawQuery("SELECT ... WHERE s_name = ? AND ..."+
"... WHERE s_name = ? AND ...",
new String[] { station1, station2 });

Android Sqlite subquery does not return any value

I am using the following query to get the list of item
Query="Select * from Item_List where idSubcategory = (Select _id from SubCategory_List where Name = ?)";
c = db.rawQuery(Query, new String[] { Search });
But it is returning no result.
I ran this query on the database which I pulled from the device as well as emulator. There I am getting proper result, But when I run it on device no data is returned.
Can you try a different way? Use execSQL() as explained in this link. It is exactly the same problem.
EDIT
Unfortunately i didn't notice that execSQL() return no data (the work day is ending, finally!). The only solution i have is to do a first query and the result of this one will be the parameter of second query.
EDIT 2
Try to add "IN" to your query, like this:
Query = "Select * from Item_List where idSubcategory in (Select _id from SubCategory_List where Name = ?)"

ORMLite how to make '<=' with two columns in same table?

I want to make one query like this:
SELECT * FROM my_table where column_one <= column_two;
With QueryBuilder I can to make where().le(column_one, Object obj), but I want some like where().le(column_one, column_two);
Actually, I want the following query:
SELECT * FROM table_one INNER JOIN table_two ON
table_one.column_foreign_id = table_two.id WHERE table_two.column_one
<= table_two.column_two.
What is the best way?
Thank you for your time.
Yes, you can do it.
Code:
QueryBuilder<Account, String> queryBuilder = accountDao.queryBuilder();
queryBuilder.where().le(Account.COLUMN_ONE_NAME,
new ColumnArg(Account.COLUMN_TWO_NAME));
List<Account> results = queryBuilder.query();
More information here: see 3.7 Using Column Arguments
Have you considered using rawQuery instead?
As the docs say:
The built-in methods available in the Dao interface and the
QueryBuilder classes don't provide the ability to handle all types of
queries.

Using Variable in sqlite

I am making a quiz app in which i have to select the number of question depending upon user choice.Actually i want to something like this
db.execute('select * from Quiz limit **Variable**')//Here i want to use variable instead of constant value.
Could you help me in this
Thanks in advance
Change your code as:
public void selectall(String variable){
Cursor c=db.rawQuery("SELECT from Quiz limit "+variable);
}
where variable is set according to user selection
NOTE : db.execute is not SQLiteDatabase class method in Android you will need to use execSQL or query methods for quering database table
and see this tutorial for performing all select,delete,update and insert operation on database in android :
http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/
Why not use something like this:
int num = 10;
db.execSQL("select * from Quiz limit " + num)

How do I join two SQLite tables with the Android API?

I like to use the query method instead of rawQuery because it is more elegant for me, but it seems that the method can receive only one table as an input parameter.
Is there a solution for many tables?
I dont think there's any other method which can offer you this flexibility. Thats why the API comes with the method rawQuery(). Use sql query using UNION to join data from two tables.
for example:
String query = "SELECT Id, Name FROM Finance " +
"UNION ALL " +
"SELECT Id, Name FROM Marketing";
Cursor c = db.rawQuery(query, null);
Best will be just using rawquery, but if you must stick with query:
( please note, I have not tested this, but I imagine it will work )
You should be able to create a view ( http://sqlite.org/lang_createview.html ) and then use the view name in place of a table name.

Categories

Resources