如何在SQLite
中加入三个表?我有三个表,一个是Info
,第二个是workForce
,第三个是workDetails
。
为了加入这三个表,我将Table Info的外键添加到Table WorkForce 和Table WorkForce的外键到表WorkDetails
表格信息:id(PK),name,status,date,weather
表WorkForce:id1(PK), subContractors,noOfPeople,noOfHours,TInfo_id(FK to table Info)
表工作详细信息:id2(PK),project,workDescription, Twf_id(FK to table workForce),TableInfo_id(FK to table Info) //contains multiple row
表格信息
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
表工作详情
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错误。我已经搜索了很长一段时间的答案,但仍然无法修复它。不确定错误是否来自查询问题。任何建议都会受到很大的影响。
答案 0 :(得分:1)
试试这个
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);
}
希望有所帮助:)
答案 1 :(得分:0)
我建议使用表别名使查询更具可读性:
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” - &gt; “WorkDetails.workDescriptionFROM”。
如果在构建查询后打印出查询,则很容易看到此问题。