I got 2 tables one is Player table and other is Matches table. I want to get the cursor to make a list of tennis matches in my MatchCursorAdapter. I have managed this by making duplicate data of columns name and picture of Players in both tables that I need to show on my list. Now I want to do this the right way just by having id of player in table matches and with id of a player I can get his name and picture form player table. I try to use INNER JOINER, but I need 2 or 4 players so I will have same column names I'm not sure how to approach this and how to than retrieve data from the cursor. Here is an image of my tables and cursor I want for more clarity This is my try but only for 1 player and I need all 4:
case MATCHES:
SQLiteQueryBuilder mQueryBuilderMatches = new SQLiteQueryBuilder();
mQueryBuilderMatches.setTables(
PlayerContract.MatchEntry.TABLE_NAME + " INNER JOIN " +
PlayerContract.PlayerEntry.TABLE_NAME + " ON " +
PlayerContract.MatchEntry.TABLE_NAME + "." +
PlayerContract.MatchEntry.COLUMN_PLAYER_1_ID + " = " +
PlayerContract.PlayerEntry.TABLE_NAME + "." +
PlayerContract.PlayerEntry._ID);
cursor = mQueryBuilderMatches.query(database, projection, selection, selectionArgs, null, null, sortOrder);
break;
This is my create method for tables:
public void onCreate(SQLiteDatabase db) {
String SQL_CREATE_ENTRIES =
"CREATE TABLE " + PlayerEntry.TABLE_NAME + " (" +
PlayerEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
PlayerEntry.COLUMN_PLAYER_NAME + " TEXT NOT NULL, " +
PlayerEntry.COLUMN_PLAYER_NATIONALITY + " TEXT, " +
PlayerEntry.COLUMN_PLAYER_YEAR_BORN + " INTEGER NOT NULL DEFAULT 0, " +
PlayerEntry.COLUMN_PLAYER_GENDER + " INTEGER NOT NULL, " +
PlayerEntry.COLUMN_PLAYER_WEIGHT + " INTEGER NOT NULL DEFAULT 0, " +
PlayerEntry.COLUMN_PLAYER_HEIGHT + " INTEGER NOT NULL DEFAULT 0, " +
PlayerEntry.COLUMN_PLAYER_PICTURE + " BLOB);";
String SQL_CREATE_ENTRIES_MATCH =
"CREATE TABLE " + MatchEntry.TABLE_NAME + " (" +
MatchEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
MatchEntry.COLUMN_PLAYER_1_ID + " INTEGER, " +
MatchEntry.COLUMN_PLAYER_2_ID + " INTEGER, " +
MatchEntry.COLUMN_PLAYER_2_TEAM_1_ID + " INTEGER, " +
MatchEntry.COLUMN_PLAYER_2_TEAM_2_ID + " INTEGER, " +
MatchEntry.COLUMN_PLAYER_1_NAME + " TEXT, " +
MatchEntry.COLUMN_PLAYER_2_NAME + " TEXT, " +
MatchEntry.COLUMN_PLAYER_1_PICTURE + " BLOB, " +
MatchEntry.COLUMN_PLAYER_2_PICTURE + " BLOB, " +
MatchEntry.COLUMN_MATCH_ARRAY_LIST + " TEXT, " +
MatchEntry.COLUMN_MATCH_TIME + " TEXT, " +
MatchEntry.COLUMN_MATCH_DATE + " TEXT, " +
MatchEntry.COLUMN_MATCH_FINISH + " INTEGER);";
db.execSQL(SQL_CREATE_ENTRIES);
db.execSQL(SQL_CREATE_ENTRIES_MATCH);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion < 2) {
db.execSQL(DATABASE_ALTER_TEAM_1_PLAYER_2_ID);
db.execSQL(DATABASE_ALTER_TEAM_2_PLAYER_2_ID);
}
}
Perhaps the following could give you an idea:-
Assume the following condensed versions (columns omitted for brevity) of your tables along with some data (12 playes) and 4 matches 2 singles and 2 doubles :-
DROP TABLE IF EXISTS players;
DROP TABLE IF EXISTS matches;
CREATE TABLE IF NOT EXISTS players (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS matches (
id INTEGER PRIMARY KEY,
match_date TEXT,
match_time TEXT,
match_type INTEGER DEFAULT 1, -- 1 = singles(default) 2 = doubles
side1_player1 INTEGER NOT NULL,
side1_player2 INTEGER,
side2_player1 INTEGER NOT NULL,
side2_player2 INTEGER
);
INSERT INTO players (name)
VALUES
('Fred'), -- player 1
('Bert'), -- player 2
('Mary'), -- 3
('Ann'), -- 4
('Mark'), -- 5
('Jane'), -- 6
('Alan'), -- 7
('Susan'), -- 8
('Charles'), -- 9
('Cathryn'), -- 10
('George'), -- 11
('Elaine'); -- 12
-- Singles matches
INSERT INTO matches (match_date, match_time, match_type, side1_player1, side2_player1)
VALUES
('2018-01-01','10:30',1,1,11), -- Fred v George
('2018-01-01','11:30',1,3,10) -- Mary v Cathryn
;
-- Doubles matches
INSERT INTO matches (match_date, match_time, match_type,side1_player1,side1_player2,side2_player1,side2_player2)
VALUES
('2018-01-01','14:00',2,1,2,7,9), -- Fred & Bert v Alan & Charles
('2018-01-01','15:00',2,4,12,6,3) -- Ann & Elaine v Jane & Mary
So the Players table looks like :-
And the Matches table looks like :-
Then :-
SELECT
CASE
WHEN match_type = 2 THEN 'Doubles'
WHEN match_type = 1 THEN 'Singles'
END AS type_of_match,
match_date, match_time,
CASE
WHEN match_type = 2 THEN
side1player1.name || ' and ' || side1player2.name ||
' V ' ||
side2player1.name || ' and ' || side2player2.name
WHEN match_type = 1 THEN
side1player1.name ||
' V ' ||
side2player1.name
END AS players
FROM matches
JOIN players AS side1player1 ON side1_player1 = side1player1.id
JOIN players AS side2player1 ON side2_player1 = side2player1.id
LEFT JOIN players AS side1player2 ON side1_player2 = side1player2.id
LEFT JOIN players AS side2player2 ON side2_player2 = side2player2.id
;
Would result in :-
Without the manipulation of derived columns the query would be :-
SELECT *
FROM matches
JOIN players AS side1player1 ON side1_player1 = side1player1.id
JOIN players AS side2player1 ON side2_player1 = side2player1.id
LEFT JOIN players AS side1player2 ON side1_player2 = side1player2.id
LEFT JOIN players AS side2player2 ON side2_player2 = side2player2.id
;
And this would produce :-
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.