Issue in Inner Join Query in Android | Trouble in Sqlite query - android

I am in trouble I have 2 table in sqlite and want both field from them but I did not got any thing. I have tried a lot on stack overflow but no one is work for me..
My query is something like that-
public List<HeadItemGroup> getAllExpense() {
List<HeadItemGroup> expenseList = new ArrayList<HeadItemGroup>();
db = sqlHp.getReadableDatabase();
String selectquery = "SELECT table_expense.item_id AS ID,table_item.item_id AS ID2,* FROM table_expense INNER JOIN table_item ON table_expense.item_id=table_item.item_id";
System.out.println("selectquery value..."+selectquery);
cursor = db.rawQuery(selectquery, null);
while (cursor.moveToNext()) {
HeadItemGroup record = new HeadItemGroup();
record.setExpId(cursor.getInt(cursor.getColumnIndex(EXPENCE_ID)));
record.setItemId(cursor.getInt(cursor.getColumnIndex(ITEM_ID)));
record.setHeadId(cursor.getInt(cursor.getColumnIndex(HEAD_ID)));
record.setDate(cursor.getString(cursor.getColumnIndex(EXPENCE_DATE)));
record.setPrice(cursor.getString(cursor.getColumnIndex(EXPENCE_PRICE)));
record.setLocationLat(cursor.getString(cursor.getColumnIndex(LOCATION_LAT)));
record.setLocationLon(cursor.getString(cursor.getColumnIndex(LOCATION_LON)));
record.setLocationName(cursor.getString(cursor.getColumnIndex(LOCATION_NAME)));
record.setCurrency(cursor.getString(cursor.getColumnIndex(CURRENCY_SYMBOL)));
record.setDescription(cursor.getString(cursor.getColumnIndex(DESCRIPTION)));
record.setItemName(cursor.getString(cursor.getColumnIndex(ITEM_NAME)));
record.setGroupId(cursor.getInt(cursor.getColumnIndex(GROUP_ID)));
expenseList.add(record);
System.out.println("total value value**************"+ cursor.getString(cursor.getColumnIndex(EXPENCE_PRICE)));
System.out.println("total value value**************"+ cursor.getString(cursor.getColumnIndex(ITEM_NAME)));
System.out.println("total value value**************"+ cursor.getString(cursor.getColumnIndex(GROUP_ID)));
}
return expenseList;
}
And when I am trying with same table for left outer join it's work..
String selectquery="select exp_id as _id, * from table_expense d left outer join table_item u on ( d.item_id=u.item_id)";
But i need all data from both table. Please help me...

Related

Sqlite how to improve performance?

I have to make more than 300 selects from my database.
Each of those queries has to be called inside of a for each loop, here's an example:
for(int id : myids){
Cursor cursor = MyDatabaseHelper.runMyQuery(id);
while(cursor.moveToNext()){
//my stuff...
}
}
MyDatabaseHelper is an instance of a database helper class, the function is like this
public Cursor runMyQuery(int id){
SQLiteDatabase db = this.getWritableDatabase();
Cursor ret = db.rawQuery("select Name, Surname, Age from PTable where Id = " + id, null);
return ret;
}
I've been told that the constant "open and close" of the db because of multiple queries it the cause of my performance issues and I should, instead, make a single query (using union etc).
Changing my code to a single query would mean changing the entire database, and I was hoping not to do that.
Is there anything I can do to improve the performance and keep the multiple selects at the same time?
Thanks
I think what you are looking for is the in clause.
Convert your myids into a string. Something like
String inClause = "(1,2,3)"
and you can use it as
"select Name, Surname, Age from PTable where Id in " + inClause
You can read more of the in operator here
You can return a single Cursor containing all the rows.
First change your runMyQuery() method to this:
public Cursor runAll(String list){
SQLiteDatabase db = this.getWritableDatabase();
String sql = "select Name, Surname, Age from PTable where " + list + " like '%,' || id || ',%'"
Cursor ret = db.rawQuery(sql, null);
return ret;
}
So you pass to the method runAll() a String which is the the comma separated list of all the ids that you have in myids and with th eoperator LIKE you compare it to each id of the table.
You create this list and get the results in a Cursor object like this:
StringBuilder sb = new StringBuilder(",");
for(int id : myids){
sb.append(String.valueOf(id)).append(",");
}
String list = sb.length() > 1 ? sb.toString() : "";
if (list.length() > 0) {
Cursor c = runAll(list);
while(c.moveToNext()){
//your stuff...
}
}

SQLite Querying with user defined data

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);`

SQLite query give different result from mysql

I have two table in my sqlite db here is the first table named supplier and here is the second table named product
what I want to do is I want to get the supplier_name in table product by selecting id_supplier in table_product. Here is my query SELECT table_supplier.id_supplier, table_supplier.supplier_name from table_invoice_in, table_supplier where table_invoice_in.id_product = '4' and table_supplier.id_supplier = table_invoice_in.id_supplier
and what I got from that query is I can get the id_supplier but supplier_name gives me 0 value, but when I try the query in mysql, I got the correct result. My question is :
Is this a limitation of sqlite or there are something wrong with my query?
Thank you in advance.
Try using a JOIN:
SELECT table_supplier.id_supplier, table_supplier.supplier_name FROM table_supplier JOIN table_invoice_in ON table_supplier.id_supplier = table_invoice_in.id_supplier WHERE table_invoice_in.id_product = '4'
The problem solved! When we try to query some column(s) from multiple table, make sure that we get the desired result by pointing directly to the column name. Here is my correct code
public Cursor SelectInvoiceInById(String id){
String query = "SELECT product_name from table_product, table_invoice_in where table_invoice_in.id_invoice_in = '"+ id + "' and table_product.id_product = table_invoice_in.id_product";
ReadDB();
cursor = database.rawQuery(query, null);
cursor.moveToFirst();
if(cursor.getCount() > 0){
while (!cursor.isAfterLast()){
Log.d("RESULT", String.valueOf(cursor.getString(cursor.getColumnIndex(SQLiteHelper.PRODUCT_NAME))));
cursor.moveToNext();
}
}
CloseDB();
return cursor;
}

Android: How to access results from Cursor when INNER JOIN is performed?

I am using INNER JOIN on two tables,table1 and table2, from my SQLite Database.
How do I access the results(columns of both tables) from the cursor? The two tables have 2 columns with same name.
String query = SELECT * FROM table1 INNER JOIN table2 ON table1.id=table2.id WHERE name like '%c%';
Cursor c = newDB.rawQuery(query, null);
You can specify column names instead of using '*'.
String query = SELECT table1.id AS ID,table2.column2 AS c2,...... FROM table1 INNER JOIN table2 ON table1.id=table2.id WHERE name like '%c%';
and then access using column name ID,c2 etc .
while (cursor.moveToNext()) {
String c2 = cursor.getString(cursor.getColumnIndex("c2"));
int id = cursor.getInt(cursor.getColumnIndex("ID"));
..............
.............
}
Editing the broken link : Check rawQuery methid here http://www.vogella.com/tutorials/AndroidSQLite/article.html
and here http://www.codota.com/android/methods/android.database.sqlite.SQLiteDatabase/rawQuery for different examples
You can access the result as you would with any other query.
The only difference is that there is a chance to name conflicts, same column name on both tables. In order to solve those conflict you would need to use the table name as a prefix.
For example
Long id = c.getLong(c.getColumnIndex(tableName1 + "." + idColumnName));
If this approach doesn't work. You should write your query as follows:
String query = SELECT table1.id AS table1_id FROM table1 INNER JOIN table2 ON table1.id=table2.id WHERE name like '%c%';
Cursor c = newDB.rawQuery(query, null);
And another general note, it is better not to use "Select *..." it is preferred to write explicitly which column you would like to select.
Cursor c=databseobject.functionname() //where query is used
if(c.movetofirst()) {
do {
c.getString(columnindex);
} while(c.movetoNext());
}
I have used the following to do an inner join:
public Cursor innerJoin(Long tablebId) {
String query = SELECT * FROM table1 INNER JOIN table2 ON table1.id=table2.id WHERE name like '%c%';
return database.rawQuery(query, null);
}
You can Iterate your cursor as below:
Cursor cursor = innerJoin(tablebId);
String result = "";
int index_CONTENT = cursor.getColumnIndex(KEY_CONTENT);
cursor.moveToFirst();
do{
result = result + cursor.getString(index_CONTENT) + "\n";
}while(cursor.moveToNext());
Hope this works for you
If you know the column name then you can find it like below,
long id = cursor.getLong(cursor.getColumnIndex("_id"));
String title = cursor.getString(cursor.getColumnIndex("title"));
if you just want to see all the columns name of the returned cursor then you can use String[] getColumnNames() method to retrieve all the column names.
Hope this will give you some hint.

Problem using variable in a Query

I am banging my head over this for several hours, I tested many ways and I know where the proble is, but I just can't figure out how to fix it, I need an external point of view here.
That's my query code:
public Cursor getAllExpensesUser(int user){
String sql = "SELECT * FROM expense WHERE _id IN (SELECT id_expense FROM forwho WHERE id_user = "+ user +")";
return mDb.rawQuery(sql, null);
}
With this query it returns nothing.
When using the following query it returns my desired rows... obviously im using the id number directly.
`String sql = "SELECT * FROM expense WHERE _id IN (SELECT id_expense FROM forwho WHERE id_user = 2)";`
I have tried also with arguments:
public Cursor getAllExpensesUser(int user){
String sql = "SELECT * FROM expense WHERE _id IN (SELECT id_expense FROM forwho WHERE id_user = ?)";
String[] arguments = {Integer.toString(user)};
return mDb.rawQuery(sql, arguments);
So bacically the problem is when I am using a variable.
Can anyone see where I am messing it up?
Thank you
It should be WHERE id_user = '" + user + "')";
SQL requires single quotes around data on the right of the equals.
try to replace
String sql = "SELECT * FROM expense WHERE _id IN (SELECT id_expense FROM forwho WHERE id_user = "+ user +")";
with
String sql = new String("SELECT * FROM expense WHERE _id IN (SELECT id_expense FROM forwho WHERE id_user = "+ String.valueOf(user) +")");
EDIT: use String.valueOf instead of Integer.toString

Categories

Resources