Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite query from multiple tables using SQLiteDatabase [duplicate]

Tags:

android

sqlite

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?

like image 594
user1049280 Avatar asked Jun 14 '12 08:06

user1049280


1 Answers

Try this:

Cursor mCursor = db.rawQuery("SELECT * FROM Table1, Table2 " +
                             "WHERE Table1.id = Table2.id_table1 " +
                             "GROUP BY Table1.data1", null);
like image 62
Graham Borland Avatar answered Oct 08 '22 05:10

Graham Borland