Muhammad Noman
Muhammad Noman

Reputation: 1366

Subquery in android sqlite, not working

// My getBus Function 
Cursor getBus(String source, String dest)
 {
     SQLiteDatabase db=this.getReadableDatabase();
     Cursor cur=db.rawQuery("SELECT b.BusName FROM BusTable b, (SELECT Route , RBusName FROM RouteBusTable WHERE Route IN ("+source+")) b1, (SELECT Route , RBusName FROM RouteBusTable WHERE Route IN ("+dest+")) c WHERE b.BusName IN (b1.RBusName) AND b.BusName IN (c.RBusName)", new String [] {});

     return cur;
 }

// Retreiving it like this
Cursor a = dbHelper.getBus("1", "12");

    a.moveToFirst();
    while (!a.isAfterLast())
    {
        String Name = a.getString(0);
        Toast.makeText(AddEmployee.this, "Stops Are "+ Name , Toast.LENGTH_LONG).show();
        a.moveToNext();
    }

It stops the app. What I doing wrong there?? Is there any mistake in subquery or retrieving??

Upvotes: 0

Views: 603

Answers (1)

sberezin
sberezin

Reputation: 3296

First of all try to quote source and dest by ' symbol like this:

"... WHERE Route IN (\'"+source+"\') ..."

If problem is here then you should think how to avoid this with multiple values in source and dest. In your particular case you should form it correctly like source = "\'1\', \'2\'" or use recommendation from here


P.S. If it doesn't work please provide logcat error message and resulting query (at runtime)

Upvotes: 1

Related Questions