【问题标题】:INNER JOIN issue with list view列表视图的 INNER JOIN 问题
【发布时间】:2017-11-26 07:08:35
【问题描述】:

我有一个列表视图,当我在 on create 中调用 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);

【问题讨论】:

    标签: android sqlite listview cursor


    【解决方案1】:

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

    让它工作的步骤

    进行选择查询

    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

    【讨论】:

    • 您的分析是 100% 正确的
    • 请添加两张表(银行、分行)的内容
    • 你没有在里面插入任何东西吗?
    • 非常感谢,问题是我声明了getLoaderManager().initLoader(1, null, this);在 onCreate 的末尾,所以我删除了它,问题解决了你有没有办法将它与你给我的查询一起使用
    • 请查看此链接stackoverflow.com/q/47943367/7994373@minasameh
    猜你喜欢
    • 2011-03-25
    • 1970-01-01
    • 2012-12-04
    • 2013-05-02
    • 1970-01-01
    • 2019-08-12
    • 2020-08-10
    • 1970-01-01
    相关资源
    最近更新 更多