在sqlite中左加入多个table
Left Join multiple table in sqlite
如何连接SQLite
中的三个表?我有三张表,一张是 Info
,第二张是 workForce
,第三张是 workDetails
.
为了连接这三个表,我将 Table 信息的 外键添加到 Table WorkForce 和 外键中Table WorkForce 到 Table WorkDetails
Table 信息:id(PK),name,status,date,weather
Table 劳动力:id1(PK), subContractors,noOfPeople,noOfHours,TInfo_id(FK to table Info)
Table 工作详情:id2(PK),project,workDescription, Twf_id(FK to table workForce),TableInfo_id(FK to table Info) //contains multiple row
Table 信息
ID NAME Weather Date Status
---------- ---------- ---------- ---------- ----------
1 Paul Sunny 15/10 MC
2 Allen Rainy 15/10 Working
Table 劳动力
ID1 SubContractors NoOfPeople NoOfHours TInfo_id
---------- -------------- ---------- ---------- -----------
1 AAA 2 2 1
2 BBB 3 1 2
Table 工作详情
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
假设名字是 Paul,那么将检索 ID 为 1 和 TableInfo_id 1 的所有行。
现在,我想使用 cursor
将它们检索到 tableView 中。下面是我的代码片段。
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;
}
}
编译器给我 无法解决 rawQuery 错误。我一直在寻找答案很长时间,但仍然无法解决。不确定错误是否来自查询问题。任何建议将不胜感激。
我建议使用 table 别名使查询更具可读性:
SELECT i.weather, i.date, i.status, w.subContractors, w.noOfPeople, w.noOfHours,
wd.project, wd.workDescription
FROM Information i LEFT JOIN
WorkForce w
ON w.TInfo_id = i.ID LEFT JOIN
WorkDetails wd
ON wd.Twf_id = WorkForce.ID1
WHERE i.Name = ?
但是,你的问题的原因是当字符串合并时,值运行在一起,例如"WorkDetails.workDescription"+"FROM " --> "WorkDetails.workDescriptionFROM "。
如果在构建查询后打印出来,您很容易发现这个问题。
试试这个
public Cursor readEntry(String name) {
return database.rawQuery("SELECT i.Weather, i.Date, i.Status, w.SubContractors, w.NumberOfPerson, w.NumberOfHours,
wd.Project, wd.WorkDescription FROM Information i LEFT JOIN WorkForce w
ON w.TInfo_id = i.ID LEFT JOIN WorkDetails wd ON wd.Twf_id = w.ID WHERE
i.Name = ?",new String[] {String.valueOf(name)},null);
}
希望对您有所帮助:)
如何连接SQLite
中的三个表?我有三张表,一张是 Info
,第二张是 workForce
,第三张是 workDetails
.
为了连接这三个表,我将 Table 信息的 外键添加到 Table WorkForce 和 外键中Table WorkForce 到 Table WorkDetails
Table 信息:id(PK),name,status,date,weather
Table 劳动力:id1(PK), subContractors,noOfPeople,noOfHours,TInfo_id(FK to table Info)
Table 工作详情:id2(PK),project,workDescription, Twf_id(FK to table workForce),TableInfo_id(FK to table Info) //contains multiple row
Table 信息
ID NAME Weather Date Status
---------- ---------- ---------- ---------- ----------
1 Paul Sunny 15/10 MC
2 Allen Rainy 15/10 Working
Table 劳动力
ID1 SubContractors NoOfPeople NoOfHours TInfo_id
---------- -------------- ---------- ---------- -----------
1 AAA 2 2 1
2 BBB 3 1 2
Table 工作详情
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
假设名字是 Paul,那么将检索 ID 为 1 和 TableInfo_id 1 的所有行。
现在,我想使用 cursor
将它们检索到 tableView 中。下面是我的代码片段。
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;
}
}
编译器给我 无法解决 rawQuery 错误。我一直在寻找答案很长时间,但仍然无法解决。不确定错误是否来自查询问题。任何建议将不胜感激。
我建议使用 table 别名使查询更具可读性:
SELECT i.weather, i.date, i.status, w.subContractors, w.noOfPeople, w.noOfHours,
wd.project, wd.workDescription
FROM Information i LEFT JOIN
WorkForce w
ON w.TInfo_id = i.ID LEFT JOIN
WorkDetails wd
ON wd.Twf_id = WorkForce.ID1
WHERE i.Name = ?
但是,你的问题的原因是当字符串合并时,值运行在一起,例如"WorkDetails.workDescription"+"FROM " --> "WorkDetails.workDescriptionFROM "。
如果在构建查询后打印出来,您很容易发现这个问题。
试试这个
public Cursor readEntry(String name) {
return database.rawQuery("SELECT i.Weather, i.Date, i.Status, w.SubContractors, w.NumberOfPerson, w.NumberOfHours,
wd.Project, wd.WorkDescription FROM Information i LEFT JOIN WorkForce w
ON w.TInfo_id = i.ID LEFT JOIN WorkDetails wd ON wd.Twf_id = w.ID WHERE
i.Name = ?",new String[] {String.valueOf(name)},null);
}
希望对您有所帮助:)