This question already has answers here:
How do I join two SQLite tables in my Android application?
(4 answers)
Closed 8 years ago.
I have 2 tables in my database, for example: Table1: id (PK), data1 and Table2: id (PK), id_table1 (FK), data2. How can I make a query like that:
SELECT * FROM Table1, Table2 WHERE Table1.id = Table2.id_table1
GROUP BY Table1.data1
I'm using SQLiteDatabase and its query() method.
Cursor mCursor = db.query(true, new String[] {"Table1","Table2"},
new String[] {"Table1.id","data1", "Table2.id", "id_table1", "data2"},
"Table1.id=Table2.id_table1", null, "Table1.data1", null,null,null);
But there's a problem with the second arg - it's only possible to use String, not String[] (like new String[] {"Table1","Table2}). What should I do to make a query from multiple tables in that way?
Try this:
Cursor mCursor = db.rawQuery("SELECT * FROM Table1, Table2 " +
"WHERE Table1.id = Table2.id_table1 " +
"GROUP BY Table1.data1", null);
So when you need to JOIN Tables, you have to use rawQuery instead of query.
So your statement
String SELECT_QUERY = SELECT * FROM Table1 t1 INNER JOIN Table2 t2 ON t1.id = t2.id GROUP BY t1.data1;
I recommend to you use JOIN because it more faster and safer then your approach. So then your rawQuery method can looks like this:
cursor = db.rawQuery(SELECT_QUERY, null);
Have look at
rawQuery in SQLiteDatabase
Regards
Related
I am stuck with one scenario in which I need to do complex query to get cursor. Don't know It is possible or not. Scenario is :
There are three tables in database.
Table Columns
Table-1 _id, name, number, ....
Table-2 _id, table1_id, col1, col2, ....
Table-3 _id, table2_id, col1, col2, ....
In these tables, when any record is inserted in table 2, corresponding table1 id is inserted in that record. Same for table2 id is inserted with table 3 record.
I want cursor for CursorAdapter to display list view of Table-3 data.
Cursor cursor = getContentResolver().query(TABLE_3_NAME, null, null, null, null);
Now need to add selection and selection args of Table-1 in this query.
Is it possible in Android?
You can use raw query to fetch from various tables. One example would be as below.
Cursor mCursor = db.rawQuery("SELECT * FROM Table-1, Table-3 " +
"WHERE Table1.id = <Whatever selection args you want> " +
"GROUP BY Table1.id", null);
This is just an example. You will have to change it as per your requirement.
You can write a SQL selection query (i.e. normal way) and you can execute using rawQuery() method.
For example:
Cursor cursor = db.rawQuery(selectQuery, null);
I have a query that joins 2 tables get the required data on my android, what picture here is the user clicks on an item and the item's ID is used to query the right data in the Database, I know I can simply use database.query() but it according to my research it is used for simply database querying only, in my case I should use rawQuery() which provides more power of the database. below is my query which links table 1 to table 2 to get the users name from table one and user last name from table 2 if the foreign key is the same as user key
Assume this is my query:
String sQuery = SELECT table1.ID, table2.userlastname FROM table1, table2 WHERE "+table1.ID = table2.foreign;
If i try to specify the user id like below it gets all data in the database table which means i should replace id with "=?" but how do I do this when I am dealing which such a query, one that uses db.rawQuery() instead of db.query()
`private Object userInfo(int id)
{
String sQuery = SELECT table1.ID, table2.userlastname
FROM table1, table2 WHERE "+table1.ID = id;
}`
Basically you replace the parameter by question marks '?' and pass them through a String array in the order they appear in the query.
String queryStr = "SELECT table1.ID, table2.userlastname
FROM table1
INNER JOIN table2 ON table1.ID = table2.foreign;
WHERE table1.ID = ?";
String[] args = new String[1];
args[0] = String.valueOf(id);
Cursor cur = db.rawQuery(queryStr, args);
it did not work until I joined table 2 like:
`String queryStr = "SELECT table1.ID, table2.userlastname
FROM table1
INNER JOIN table2 ON table1.ID = table2.foreign
WHERE table1.ID = ?";
String[] args = new String[1];
args[0] = String.valueOf(id);
Cursor cur = db.rawQuery(queryStr, args);`
This question already has answers here:
How do I join two SQLite tables in my Android application?
(4 answers)
Closed 9 years ago.
I have 3 tables in my sqllite database
Two are config tables and one is main table where my data is stored.
now how to retrive data from main table by joining 2 tables in android, How and where to perform join operations.
Can anyone guide me.
you can just execute a rawQuery.
For example something like this:
db.rawQuery("SELECT a.*
FROM table_1 a
INNER JOIN table_2 b ON a.id=b.anyId
INNER JOIN table_3 c ON b.id= c.anyId
WHERE c.key = ?", new String[]{"test"});
The first parameter is the query you want to execute. For all your keys you want to add to your query just add an ? in the query.
The second parameter is a String Array. In this array you put your keys, like the example given above the value test.
EDIT:
it's also possible to use rawQuery for update, insert or delete.
For example one simple update query:
db.rawQuery("UPDATE table_1
SET fieldA = ?,
fieldB = ?
WHERE id = ?", new String[]{"test", "test2", "1"});
class DatabaseHelper extends SQLiteOpenHelper
write below code in your funcions...
SQLiteDatabase db = this.getReadableDatabase();
private final String MY_QUERY = "YOUR QUERY";
db.rawQuery(MY_QUERY, new String[]{"Parameter"});
db.close();
You need rawQuery method for solve this issue.
private final String MY_QUERY = "SELECT * FROM table_a a INNER JOIN table_b b ON a.id=b.other_id WHERE b.property_id=?";
db.rawQuery(MY_QUERY, new String[]{String.valueOf(propertyId)});
But if you want to do a good implementation create a SQLiteHelper and DataSource for each table and do the relationship in the datasource.
what do you think my problem is.
I am querying a sqlite database.
This code doesn't give any result.
String sql = " select _id from MYTABLE where _id = ? ";
Cursor cur = mDb.rawQuery(sql, new String[] {"5653"}) ;
but if I am executing the query without parameters like this:
String sql = " select _id from MYTABLE where _id = 5653 ";
Cursor cur = mDb.rawQuery(sql, null) ;
One row is returned as expected.
Many thanks.
That happens because the values are bound as strings, and that column is (i am guessing) an int. so the where clause will end up being
where _id = "5653"
From rawQuery javadoc for selectionArgs -
You may include ?s in where clause in the query, which will be replaced by the values from selectionArgs. The values will be bound as Strings.
Try this:
String sql = " select _id from MYTABLE where _id = '5653' ";
I have two tables in my database table1, table2. I have a join query like this:
select a.*,b.* from table1 a,table2 b where a.field1=b.field1;
Its working fine while i test it in sqlite manager(sqlite browser). when i try to make it through java like this:
database.execSQL("select a.*,b.* from table1 a,table2 b where a.field1=b.field1;");
its saying return type is not cursor. How can i get it to cursor with join. I have to get that data and show in a listview
use like this:
String qry = "select a.*,b.* from table1 a,table2 b where a.field1=b.field1";
Cursor cur = db.rawQuery(qry,null);
String query = "select a.*,b.* from table1 a,table2 b where a.field1=b.field1;";
Cursor c = database.rawQuery(query, null);
/* do whatever you want with 'c' */
Following is an Example of rawQuery, you can add your Query Respectively.
String getRT = "SELECT count(*) from "+ DATABASE_PROFILE+";";
Cursor mCur = sqlitedb.rawQuery(getRT, null);
return mCur;
SQLiteDatabase.rawQuery(query, selectionArgs)
Use rawQuery method to design any sort of complex query you might need. In the reference there are many useful methods you might need.