将数据传递给另一个类

时间:2015-10-02 05:07:58

标签: java android sqlite foreign-keys

我有两个表,一个是Information,另一个是WorkDetailsTable。根据意图,活动流程为Information.java>>WorkDetailsTable.java。单击button中的Information.java后,它会保存所有数据并传递给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)");
    }

为了使我的问题更容易理解,我将其总结如下:

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? 

我问了一个与我的问题有关的类似问题。请参考Inserting a foreign key into a table

当我运行我的项目时,它会出错。

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)

3 个答案:

答案 0 :(得分:1)

问题在于您的创建查询,第二个问题。

  

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 )“);

您的日志显示这是执行的查询:

  

android.database.sqlite.SQLiteException:near“tableWork”:语法错误(代码1):,同时编译: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)

首先,你错过了桌子后的空间。其次,您的常量TABLE_WORKDETAILS值为Work Details。表名不应包含空格,因此请将其更改为Work_Details。您还应该在FOREIGN KEY(TableInfo_id)之后给出空格。

应该是这样的:

 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))");

答案 1 :(得分:1)

您收到的错误是因为您使用的SQL语句不正确:

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)");

在单词table后面你缺少一个空格,你在句子的末尾也缺少一个括号。

应该是这样的:

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))");

我认为外键语法是正确的,但请注意,默认情况下禁用外键限制,如果要启用它们,则必须使用PRAGMA foreign_keys = ON句子。

最后,您的变量TABLE_WORKDETAILS应该没有空格,因为表名不能包含它们,因此将其更改为Work_Details 希望它有所帮助

答案 2 :(得分:0)

为了避免这种耗时且容易出错的低级别sql问题,您可以考虑使用sql-lite ORM(对象关系映射)库来处理对象的持久性(从而创建sql语句)对你而言。

要概述,请查看hereherehere