Search code examples
androidsqlitelistviewcursor

INNER JOIN issue with list view


I have a listview and when I call the SQL query inner join in the on create it doesn't do anything and also no errors occurred

        public Cursor getBankBranch() {
    Cursor cursor;
    SQLiteDatabase db = mDbHelper.getReadableDatabase();
    String selectQuery = "SELECT  * FROM " + DataEntry.BRANCH_TABLE_NAME + " INNER JOIN  " 
            + DataEntry.BANK_TABLE_NAME + " ON " 
            + DataEntry.BANK_TABLE_NAME + "." + DataEntry.COLUMN_BANK_ID + "  = " 
            + DataEntry.BRANCH_TABLE_NAME + "." + DataEntry.COLUMN_BRANCH_BANK_ID;
    cursor = db.rawQuery(selectQuery, null);
    cursor.moveToFirst();
    cursor.close();
    return cursor;
}

this is the table's content

            String SQL_CREATE_BANK_TABLE = "CREATE TABLE " + DataEntry.BANK_TABLE_NAME + " ("
            + DataEntry.COLUMN_BANK_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + DataEntry.COLUMN_BANK_NAME + " TEXT );";

    String SQL_CREATE_BRANCH_TABLE = "CREATE TABLE " + DataEntry.BRANCH_TABLE_NAME + " ("
            + DataEntry.COLUMN_BRANCH_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + DataEntry.COLUMN_BRANCH_NAME + " TEXT , "
            + DataEntry.COLUMN_BRANCH_NUMBER + " TEXT , "
            + DataEntry.COLUMN_BRANCH_ADDRESS + " TEXT, "
            + DataEntry.COLUMN_BRANCH_BANK_ID + " INTEGER );";

    // Execute the SQL statement
    db.execSQL(SQL_CREATE_BANK_TABLE);
    db.execSQL(SQL_CREATE_BRANCH_TABLE);

Solution

  • you are just getting the cursor that points to the result set, then you move it to the first result after that you close it, but you don't actually use it.

    the steps of getting it to work

    make a select query

    String selectQuery = "SELECT ..."
    

    execute the query to get cursor

    cursor = db.rawQuery(selectQuery, null);
    

    create a cursorAdapter

    MyCursorAdapter adapter = new MyCursorAdapter(this, cursor);
    

    set the adapter of the ListView

    list.setAdapter(adapter);
    

    here is an example of how to use it