我在SQLite中有两个表(Information和WorkDetails),其中WorkDetails有一个引用Information的外键。每次插入数据时,外键都应该遵循信息中PK的数量。但是,我在外键列中得到NULL值。信息和工作详细信息表中的PK是自动增量。
MyDatabaseAdapter.java
$total += $totalprice.
WorkDetailsTable.java
public void onCreate(SQLiteDatabase db)
{
db.execSQL("create table "+TABLE_INFO+"(ID INTEGER PRIMARY KEY ,Name TEXT)");
db.execSQL("create table"+TABLE_WORKDETAILS+"(ID INTEGER PRIMARY KEY , Project TEXT, WorkDescription TEXT, Per Text, TimeIn DATETIME, TimeOut DATETIME,TotalHours DATETIME, TableInfo_id INTEGER, FOREIGN KEY(TableInfo_id)REFERENCES TABLE_INFO(ID)");
}
InfoAPI.java
WD= new com.example.project.project.API.WorkDetailsAPI(this);
ts= new com.example.project.project.API.InfoAPI(this);
Button btn1=(Button)findViewById(R.id.button2);
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
W1=txtWork1.getText().toString();
W2=txtWork2.getText().toString();
W3=txtWork3.getText().toString();
W4=txtWork4.getText().toString();
a1 = spinnerTra.getSelectedItem().toString();
a2= spinnerTra2.getSelectedItem().toString();
a3 = spinnerTra3.getSelectedItem().toString();
a4=spinnerTra4.getSelectedItem().toString();
P1=per1.getText().toString();
P2=per2.getText().toString();
P3=per3.getText().toString();
P4=per4.getText().toString();
ts.insertTimeSheet(name); // refer to TimeSheetAPI
WD.insertWorkDetails(a1,W1,P1,b,c,th); // insert multiple row and refer to WorkDetailsAPI
WD.insertWorkDetails(a2,W2,P2,d,e1,th);
WD.insertWorkDetails(a3, W3, P3, f, g,th);
WD.insertWorkDetails(a4,W4,P4,h,i,th);
}
});
WorkDetailsAPI.java
public class InfoAPI {
private SQLiteDatabase database;
private MyDatabaseHelper dbHelper;
public String[] allColumns={MyDatabaseHelper.ID,MyDatabaseHelper.Name};
public InfoAPI(Context context)
{
dbHelper=new MyDatabaseHelper(context);
}
public void open() throws SQLException {
database = dbHelper.getWritableDatabase();
}
public void close() {
dbHelper.close();
}
public long insertTimeSheet(String name)
{
database=dbHelper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put(MyDatabaseHelper.Name,name);
database.insert(MyDatabaseHelper.TABLE_INFO,null,values);
database.close();
return 0 ;
}
}
答案 0 :(得分:0)
在“insertTimeSheet”方法中实际插入父行后,需要将插入的父行ID(TABLE_INFO ID)传递给“insertWorkDetails”方法。
public long insertWorkDetails(Integer id, String project, String workDescription, String per,String timeIn,String timeOut,String totalHours){ ... }
外键不会自行更新。
答案 1 :(得分:0)
更改这样的插入方法,以便获得table_info id
public long insertTimeSheet(String name)
{
database=dbHelper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put(MyDatabaseHelper.Name,name);
database.insert(MyDatabaseHelper.TABLE_INFO,null,values);
Cursor cursor = database.rawQuery("SELECT MAX(ID) FROM Table_Info", null);
database.close();
return cursor.getLong(0) ;
}
并更改您的代码
long id = ts.insertTimeSheet(name); // refer to TimeSheetAPI
WD.insertWorkDetails(a1,W1,P1,b,c,th,id);
你的insertwd应该
public long insertWorkDetails(String project, String workDescription, String per,String timeIn,String timeOut,String totalHours, long id)
{
database=dbHelper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put(MyDatabaseHelper.Project,project);
values.put(MyDatabaseHelper.WorkDescription,workDescription);
values.put(MyDatabaseHelper.Per,per);
values.put(MyDatabaseHelper.TimeIn,timeIn);
values.put(MyDatabaseHelper.TimeOut,timeOut);
values.put(MyDatabaseHelper.TotalHours, totalHours);
values.put("TableInfo_id", id);
database.insert(MyDatabaseHelper.TABLE_WORKDETAILS,null,values);
database.close();
return 0 ;
}