dimanche 11 octobre 2015

Left Join multiple table in sqlite

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.

MyDatabaseHelper.java

public class MyDatabaseHelper extends SQLiteOpenHelper {
    public static final int DATABASE_VERSION=1;
    public static final String DATABASE_NAME="131.db";
    public static final String TABLE_INFO="Information";
    public static final String TABLE_WORKFORCE="WorkForce";
    public static final  String TABLE_WORKDETAILS="WorkDetails";
    public static final String Subcontractors="subcontractors";
    public static final String NumberOfPerson="numberOfPerson";
    public static final String NumberOfHours="numberOfHours";
    public static final String ID="id";
    public static final String ID1="id1";
    public static final String ID2="id2";
    public static final String Name="name";
    public static final String Weather="weather";
    public static final String Date="date";
    public static final String Project="project";
    public static final String WorkDescription="workDescription";
    public static final String Status="status";
    public static final String TableInfo_id="tableInfo_id";
    public static final String TInfo_id="tInfo_id";
    public static final String Twf_id="tWf_id";



    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table " + TABLE_INFO + "(ID INTEGER PRIMARY KEY ,Name TEXT,Weather TEXT, Date DATETIME, Status Text)");
        db.execSQL("create table " + TABLE_WORKFORCE + "(ID INTEGER PRIMARY KEY  ,Subcontractors TEXT,NumberOfPerson INTEGER,NumberOfHours TEXT,TInfo_id INTEGER, FOREIGN KEY(TInfo_id) REFERENCES "+TABLE_INFO+"(ID))");
        db.execSQL("create table " + TABLE_WORKDETAILS + "(ID INTEGER PRIMARY KEY , Project TEXT, WorkDescription TEXT,TableInfo_id INTEGER, Twf_id INTEGER, FOREIGN KEY(Twf_id) REFERENCES "+TABLE_WORKFORCE+"(ID), FOREIGN KEY(TableInfo_id) REFERENCES "+TABLE_INFO+"(ID))");
    }

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) {

  //  String selectQuery = ("SELECT Weather,Date,Status,SubContractors,NumberOfPeople,NumberOfHours,TimeIn,TimeOut FROM "+
    //       MyDatabaseHelper.TABLE_INFO+MyDatabaseHelper.TABLE_WORKFORCE+MyDatabaseHelper.TABLE_WORKDETAILS+ "WHERE Name= ? AND"+MyDatabaseHelper.ID=MyDatabaseHelper.ID1+ "AND"+MyDatabaseHelper.ID=MyDatabaseHelper.TableInfo_id);
   return database.rawQuery("SELECT Information.weather, Information.date,Information.status"+"WorkForce.subContractors," +
           "WorkForce.noOfPeople,WorkForce.noOfHours"+"WorkDetails.project,WorkDetails.workDescription"+"FROM Information LEFT JOIN WorkForce" +
           " ON WorkForce.TInfo_id=Information.ID LEFT JOIN WorkDetails ON WorkDetails.Twf_id=WorkForce.ID1 WHERE Information.Name =?");

  //  if (c != null) {
    //    c.moveToFirst();
   // }
   // return c;

}

}

The compiler give me cannot resolve rawQuery error. I've been searching the answer for quite a long time but still cannot fix it. Not sure whether the error is came from query problem or not. Any suggestion would be greatly appreaciated.

Aucun commentaire:

Enregistrer un commentaire