根据两个不同的ID获取记录

时间:2018-05-31 21:20:02

标签: javascript android sqlite recordset

当我只有一个要搜索的ID时,我能够从SQLite数据库中获取记录。例如,我已将我的联赛身份证传递给保龄球桌,我列出了与此身份证相关联的所有投球手。现在,当我为这个投球手创建一个系列时,我将联盟ID和投球手ID传递给系列赛表,以确保我只获得属于某个联赛中投球手的系列赛。

为了列出保龄球,我使用了以下代码;

public Bowler getBowler(String leagueId) {
        //Get Readable Database If We Are Not Inserting Anything
        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.query(Bowler.TABLE_NAME,
                new String[]{Bowler.COLUMN_ID, Bowler.COLUMN_LEAGUE_ID, Bowler.COLUMN_NAME, Bowler.COLUMN_TIMESTAMP},
                Bowler.COLUMN_LEAGUE_ID + "=?",
                new String[]{String.valueOf(leagueId)}, null, null, null, null);

        if (cursor != null)
            cursor.moveToFirst();

        //Prepare Bowler Object
        Bowler bowler = new Bowler(
                cursor.getInt(cursor.getColumnIndex(Bowler.COLUMN_ID)),
                cursor.getString(cursor.getColumnIndex(Bowler.COLUMN_LEAGUE_ID)),
                cursor.getString(cursor.getColumnIndex(Bowler.COLUMN_NAME)),
                cursor.getString(cursor.getColumnIndex(Bowler.COLUMN_TIMESTAMP)));

        //Close Database Connection
        cursor.close();

        return bowler;
    }

要列出保龄球系列,我尝试了以下内容;

public Series getSeries(String leagueId, String bowlerId) {
        //Get Readable Database If We Are Not Inserting Anything
        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.query( Series.TABLE_NAME,
                new String[]{Series.COLUMN_ID, Series.COLUMN_LEAGUE_ID, Series.COLUMN_BOWLER_ID, Series.COLUMN_NAME, Series.COLUMN_SERIES_AVERAGE, Series.COLUMN_TIMESTAMP},
                Series.COLUMN_LEAGUE_ID + Series.COLUMN_BOWLER_ID + "=?",
                new String[]{String.valueOf(leagueId)}, null, null, null, null)
                new String[]{String.valueOf(bowlerId)}, null, null, null, null);

        if (cursor != null)
            cursor.moveToFirst();

        //Prepare Bowler Object
        Series series = new Series(
                cursor.getInt(cursor.getColumnIndex(Series.COLUMN_ID)),
                cursor.getString(cursor.getColumnIndex(Series.COLUMN_LEAGUE_ID)),
                cursor.getString(cursor.getColumnIndex(Series.COLUMN_BOWLER_ID)),
                cursor.getString(cursor.getColumnIndex(Series.COLUMN_NAME)),
                cursor.getString(cursor.getColumnIndex(Series.COLUMN_SERIES_AVERAGE)),
                cursor.getString(cursor.getColumnIndex(Series.COLUMN_TIMESTAMP)));

        //Close Database Connection
        cursor.close();

        return series;
    }

我知道这是不正确的,我不知道如何正确地将这种格式化。

我不确定这是否是正确的方法,或者如果有其他方法可以完成同样的事情,那可能会更容易。

1 个答案:

答案 0 :(得分:0)

看起来您的选择查询不正确。您正尝试从两个单独的列中进行选择。

你有: “WHERE seri_idbowler_id =?”

你想要的是“ “WHERE seri_id =?AND bowler_id =?”

尝试格式化您的查询,如下所示:

db.query( Series.TABLE_NAME,
            new String[]{Series.COLUMN_ID, Series.COLUMN_LEAGUE_ID, Series.COLUMN_BOWLER_ID, Series.COLUMN_NAME, Series.COLUMN_SERIES_AVERAGE, Series.COLUMN_TIMESTAMP},
            Series.COLUMN_LEAGUE_ID + "=?" + " AND " +  Series.COLUMN_BOWLER_ID + "=?",
            new String[]{String.valueOf(leagueId), String.valueOf(bowlerId)}, null, null, null, null);