I have two tables,one is Information
and another is WorkDetailsTable
. The flow of activity is Information.java>>WorkDetailsTable.java
by intent. When button
in Information.java
has clicked, it will save all the data and pass to WorkDetails.java
.
Information.java
Button button=(Button)findViewById(R.id.button5);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent(context, WorkDetailsTable.class);
intent.putExtra("a",a);
intent.putExtra("b",b);
intent.putExtra("date1",date1);
intent.putExtra("c",c);
startActivity(intent);
}
});
WorkDetailsTable
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final String name=getIntent().getExtras().getString("a"); // data get from Information.java
final String weather=getIntent().getExtras().getString("b");
final String date=getIntent().getExtras().getString("date1");
final String status=getIntent().getExtras().getString("c");
ts= new com.example.project.project.API.InfoAPI(this);
WD= new com.example.project.project.API.WorkDetailsAPI(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();
P1=per1.getText().toString();
P2=per2.getText().toString();
P3=per3.getText().toString();
P4=per4.getText().toString();
ts.insertTimeSheet(name,weather,date,status);
insert multiple row to Work Details table, Id auto increment but foreign key remains the same
WD.insertWorkDetails(a1,W1,P1,b,c,th);
WD.insertWorkDetails(a2,W2,P2,d,e1,th);
WD.insertWorkDetails(a3, W3, P3, f, g,th);
WD.insertWorkDetails(a4,W4,P4,h,i,th);
}
});
}
InfoAPI.java
public String[] allColumns={MyDatabaseHelper.ID,MyDatabaseHelper.Name,MyDatabaseHelper.Weather,MyDatabaseHelper.Date,MyDatabaseHelper.Status};
public long insertTimeSheet(String name,String weather,String date,String status)
{
database=dbHelper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put(MyDatabaseHelper.Name,name);
values.put(MyDatabaseHelper.Weather,weather);
values.put(MyDatabaseHelper.Date,date);
values.put(MyDatabaseHelper.Status,status);
database.insert(MyDatabaseHelper.TABLE_INFO,null,values);
database.close();
return 0 ;
}
WorkDetailsAPI.java
public String[] allColumns={MyDatabaseHelper.ID2,MyDatabaseHelper.Project,MyDatabaseHelper.WorkDescription,MyDatabaseHelper.Per,MyDatabaseHelper.TimeIn,MyDatabaseHelper.TimeOut,MyDatabaseHelper.TotalHours,MyDatabaseHelper.TableInfo_id};
public long insertWorkDetails(String project, String workDescription, String per,String timeIn,String timeOut,String totalHours)
{
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);
database.insert(MyDatabaseHelper.TABLE_WORKDETAILS,null,values);
database.close();
return 0 ;
}
MyDatabaseHelper.java
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_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)");
}
To make my question more understandable, I have summarized it as below:
1. Is this the correct way to pass the data to another class and save them to different table ?
2. How to insert a foreign key into another table?
I have asked a similar question which related to my problem. Kindly refer Inserting a foreign key into a table
When I run my project, it comes out with errors.
10-02 04:56:29.104 5016-5030/com.example.project.project E/Surface﹕ getSlotFromBufferLocked: unknown buffer: 0xb4057be0
10-02 04:56:29.107 5016-5030/com.example.project.project D/OpenGLRenderer﹕ endAllStagingAnimators on 0xa2d54000 (RippleDrawable) with handle 0xabe46820
10-02 04:56:34.936 5016-5016/com.example.project.project E/SQLiteLog﹕ (1) near "tableWork": syntax error
10-02 04:56:34.974 5016-5016/com.example.project.project D/AndroidRuntime﹕ Shutting down VM
10-02 04:56:34.974 5016-5016/com.example.project.project E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.project.project, PID: 5016
android.database.sqlite.SQLiteException: near "tableWork": syntax error (code 1): , while compiling: create tableWork Details(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)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
Aucun commentaire:
Enregistrer un commentaire