INNER JOIN问题与列表视图

时间:2017-11-26 07:08:36

标签: android sqlite listview cursor

我有一个listview,当我在on on中调用SQL查询内部联接时,它没有做任何事情也没有发生错误

        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;
}

这是表的内容

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

1 个答案:

答案 0 :(得分:0)

您只是将光标指向结果集,然后在关闭它之后将其移动到第一个结果,但您实际上并未使用它。

让它发挥作用的步骤

进行选择查询

String selectQuery = "SELECT ..."

执行查询以获取游标

cursor = db.rawQuery(selectQuery, null);

创建一个cursorAdapter

MyCursorAdapter adapter = new MyCursorAdapter(this, cursor);

设置ListView的适配器

list.setAdapter(adapter);

here is an example of how to use it