How to join three tables in SQLite? I have three tables, one is Info, second is workForce and third is workDetails.
In order to join this three tables, I have added foreign key of Table Info into Table WorkForce and foreign key of Table WorkForce into Table WorkDetails
Table Info:id(PK),name,status,date,weather
Table WorkForce: id1(PK), subContractors,noOfPeople,noOfHours,TInfo_id(FK to table Info)
Table WorkDetails:id2(PK),project,workDescription, Twf_id(FK to table workForce),TableInfo_id(FK to table Info) //contains multiple row
Table Info
ID NAME Weather Date Status
---------- ---------- ---------- ---------- ----------
1 Paul Sunny 15/10 MC
2 Allen Rainy 15/10 Working
Table WorkForce
ID1 SubContractors NoOfPeople NoOfHours TInfo_id
---------- -------------- ---------- ---------- -----------
1 AAA 2 2 1
2 BBB 3 1 2
Table WorkDetails
ID2 Project WorkDescription TableInfo_id Twf_id
---------- ---------- -------------- ---------- ----------
1 A B 1 1
2 1 1
3 1 1
4 1 1
5 C D 2 2
6 2 2
7 2 2
8 2 2
Assume the name is Paul, so all the row with ID 1 and TableInfo_id 1 will be retrieved.
AND now, I want to retrieve them out into tableView by using cursor. Below are my code snippet.
DisplayData.java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.displaydata);
MyDatabaseHelper db = new MyDatabaseHelper(this);
InfoAPI I1 = new InfoAPI(this);
sqlcon = new InfoAPI(this);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
table_layout = (TableLayout) findViewById(R.id.tableLayout1);
final String name1 = getIntent().getExtras().getString("name"); //the name were pass from another activity
BuildTable(name1);
}
private void BuildTable(String name)
{
sqlcon.open();
Cursor c = sqlcon.readEntry(name);
int rows = c.getCount();
int cols = c.getColumnCount();
c.moveToFirst();
// outer for loop
for (int i = 0; i < rows; i++) {
TableRow row = new TableRow(this);
row.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
// inner for loop
for (int j = 0; j < cols; j++) {
TextView tv = new TextView(this);
tv.setLayoutParams(new TableRow.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
));
tv.setBackgroundResource(R.drawable.cell_shape);
tv.setGravity(Gravity.CENTER);
tv.setTextSize(18);
tv.setPadding(0, 5, 0, 5);
tv.setText(c.getString(j));
row.addView(tv);
}
c.moveToNext();
table_layout.addView(row);
}
sqlcon.close();
}
}
InfoAPI.java
public Cursor readEntry(String name) {
Cursor c = database.rawQuery("Select status,date,weather,subcontractors,noOfPeople,noOfHours,project,workDescription
from TableInfo tf LEFT JOIN Table WorkForce twf ON
twf.tf_id = tf.id LEFT JOIN Table WorkDetails twd ON
twd.twf_id = twf.id WHERE name= ?"); // cannot resolve method rawWQuery
if (c != null) {
c.moveToFirst();
}
return c;
}
Can someone help me to see my code? I get confused in this case. Thanks
Aucun commentaire:
Enregistrer un commentaire