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)
The error you are getting is because the SQL sentence you are using is incorrect:
Here after the word
table
you are missing a blank space, also you are missing a parenthesis at the end of the sentence.It should be like this:
I think that the foreign key syntax is correct but be aware that foreign keys restrictions are disables by default, if you want to enable them you have to use
PRAGMA foreign_keys = ON
sentence.Finally your variable
TABLE_WORKDETAILS
should have no blank spaces as table names cannot have them so change it toWork_Details
Hope it helpsTo avoid such time consuming and error prone low level sql-issues, you could consider using an sql-lite ORM (Object-Relational Mapping) library, which handles the persistance of your objects (and therefore the creation of sql statements) for you.
To get an overview, take a look here, here or here
The problem is in your create query, the second one.
You logs show that this is the query which is executed:
Firstly, you missed the space after table. Secondly, your constant
TABLE_WORKDETAILS
value isWork Details
. The table name should not have spaces, so change it toWork_Details
. You should also give space afterFOREIGN KEY(TableInfo_id)
.It should be like this: