In my database I have multiple tables but my question pertains to 3 particular tables.
REQUIREMENTS table
id (Long) Primary Key
name (String) - Requirement name/description
PROJECTS table
id (Long) Primary Key
project (String) - Project Name
description (String) - Project Description
PROJECT_REQUIREMENTS table
id (Long) Primary Key
project_id (Long) Foreign Key from Projects
requirement_id (Long) Foreign Key from Requirements
mandatory (Integer) to represent Boolean value
What I want to present to the user in one activity is a Project and a checkbox list of all the Requirements not already assigned to that project. Essentially what I'm trying to do is SELECT all requirements in the requirements table WHERE the requirement is not already linked to that project AND it doesn't belong to another project.
I've tried a few variants without using a nested query yet - I'm a beginner to both Android development and SQLite so I've been going around in circles trying to work out how to do this essentially.
What I've done to validate output from queries I have tried (test code only generating logcat output) is generate a search for the requirements currently associated with the project and loaded it's key into an ArrayList, then I've done a search for ALL requirements in the Requirements table and loaded their primary keys into another Arraylist.
Then I've used the removeAll feature of the ArrayList collection to remove the project's requirements and this leaves me with the ArrayList of requirement ID's not currently assigned.
From there I run another query to get the Requirement names as a String.
Extract from the code I wrote to validate my query results
String LOGTAG = "DEBUGGING";
// Gets all primary keys of the requirements
List<Long> myRequirementIDs = datasource.getAllRequirementIds();
Log.i(LOGTAG, "Req ID's: " + Arrays.toString(myRequirementIDs.toArray()));
// Gets all requirement IDs associated with the Project myProject
List<Long> myProjReqIDs = datasource.getAllProjReqsByProj(myProject.getId());
Log.i(LOGTAG, "Proj Req ID's: " + Arrays.toString(myProjReqIDs.toArray()));
// Remove all associated project requirements from the requirements ID ArrayList
myRequirementIDs.removeAll(myProjReqIDs);
Log.i(LOGTAG, "Req ID's is now: " + Arrays.toString(myReqIDs.toArray()));
// Loop through each remaining Requirement ID and get it's name
for (Long myID : myReqIDs) {
Log.i(LOGTAG, "ReqID:" + myID + " is " + datasource.getRequirementNameById(myID));
}
Any suggestions are greatly appreciated
Aucun commentaire:
Enregistrer un commentaire