根据两个不同的 ID 获取记录
Getting Records Based Off Of Two Different ID's
在只有一个ID可以查询的情况下,我已经能够从SQLite数据库中获取记录了。例如,我已将我的联盟 ID 传递给保龄球手 Table,并且我列出了与此 ID 关联的所有保龄球手。现在,当我为这个投球手创建一个系列时,我将联盟 ID 和投球手 ID 传递给系列 table 以确保我只获得属于某个联盟中那个投球手的系列。
为了列出投球手,我使用了以下代码;
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;
}
我知道这是不正确的,但我不知道如何正确设置格式。
我不确定这是否是正确的方法,或者是否有另一种方法可以更容易地完成同样的事情。
您的 selection 查询似乎不正确。您正在尝试从两个单独的列中 select。
你有:
"WHERE series_idbowler_id =?"
你想要的是"
"WHERE series_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);
在只有一个ID可以查询的情况下,我已经能够从SQLite数据库中获取记录了。例如,我已将我的联盟 ID 传递给保龄球手 Table,并且我列出了与此 ID 关联的所有保龄球手。现在,当我为这个投球手创建一个系列时,我将联盟 ID 和投球手 ID 传递给系列 table 以确保我只获得属于某个联盟中那个投球手的系列。
为了列出投球手,我使用了以下代码;
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;
}
我知道这是不正确的,但我不知道如何正确设置格式。
我不确定这是否是正确的方法,或者是否有另一种方法可以更容易地完成同样的事情。
您的 selection 查询似乎不正确。您正在尝试从两个单独的列中 select。
你有: "WHERE series_idbowler_id =?"
你想要的是" "WHERE series_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);