gpt4 book ai didi

java - 将数据传递给另一个类

转载 作者:行者123 更新时间:2023-12-02 04:19:57 25 4
gpt4 key购买 nike

我有两个表,一个是Information,另一个是WorkDetailsTable。 Activity 的流程是 Information.java>>WorkDetailsTable.java 的 Intent 。当单击Information.java中的button时,它将保存所有数据并传递给WorkDetails.java

信息.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);
}
});

工作详细信息表

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)

最佳答案

问题出在您的创建查询中,即第二个查询。

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

首先,你错过了表格后面的空格。其次,您的常量 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))");

关于java - 将数据传递给另一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32900877/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com