数据传递到另一个类 [英] Pass data to another class
问题描述
我有两个表,一个是信息
,另一个是 WorkDetailsTable
。活动的流程 Information.java>> WorkDetailsTable.java
按意图。当按钮
在 Information.java
点击了,它会保存所有数据,并传递给 WorkDetails.java
。
Information.java
按钮按钮=(按钮)findViewById(R.id.button5); button.setOnClickListener(新View.OnClickListener(){
公共无效的onClick(查看为arg0){
意向意图=新意图(背景下,WorkDetailsTable.class);
intent.putExtra(一,一);
intent.putExtra(b的,二);
intent.putExtra(日期1,DATE1);
intent.putExtra(C,C); startActivity(意向);
}
});
WorkDetailsTable
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
最终的字符串名称= getIntent()getExtras()的getString(A)。;从Information.java //获取数据
最终字符串天气= getIntent()getExtras()的getString(B)。;
。最后弦乐日期= getIntent()getExtras()的getString(日期1);
。最后弦乐状态= getIntent()getExtras()的getString(C)。
TS =新com.example.project.project.API.InfoAPI(本);
WD =新com.example.project.project.API.WorkDetailsAPI(本);
按钮BTN1 =(按钮)findViewById(R.id.button2);
btn1.setOnClickListener(新View.OnClickListener(){
公共无效的onClick(查看为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(姓名,天气,日期,状态);
//插入多个行工作内容表,身份证自动增加,但外键保持不变
WD.insertWorkDetails(A1,W 1,P 1,B,C,TH);
WD.insertWorkDetails(A2,W2,P2,D,E1,TH);
WD.insertWorkDetails(A3,W3,P3,F,G,日);
WD.insertWorkDetails(A4,W4,P4,H,I,TH);
}
});
}
InfoAPI.java
公共字符串[] allColumns={MyDatabaseHelper.ID,MyDatabaseHelper.Name,MyDatabaseHelper.Weather,MyDatabaseHelper.Date,MyDatabaseHelper.Status};
众长insertTimeSheet(字符串名称,字符串天气,日期字符串,字符串状态)
{
数据库= dbHelper.getWritableDatabase();
ContentValues值=新ContentValues();
values.put(MyDatabaseHelper.Name,名);
values.put(MyDatabaseHelper.Weather,天气);
values.put(MyDatabaseHelper.Date,日期);
values.put(MyDatabaseHelper.Status,地位);
database.insert(MyDatabaseHelper.TABLE_INFO,空,价值);
database.close();
返回0; }
WorkDetailsAPI.java
公共字符串[] allColumns={MyDatabaseHelper.ID2,MyDatabaseHelper.Project,MyDatabaseHelper.WorkDescription,MyDatabaseHelper.Per,MyDatabaseHelper.TimeIn,MyDatabaseHelper.TimeOut,MyDatabaseHelper.TotalHours,MyDatabaseHelper.TableInfo_id};
众长insertWorkDetails(字符串项目,字符串workDescription,每串,串timeIn,超时字符串,字符串totalHours)
{
数据库= dbHelper.getWritableDatabase();
ContentValues值=新ContentValues();
values.put(MyDatabaseHelper.Project,项目);
values.put(MyDatabaseHelper.WorkDescription,workDescription);
values.put(MyDatabaseHelper.Per,份);
values.put(MyDatabaseHelper.TimeIn,timeIn);
values.put(MyDatabaseHelper.TimeOut,超时);
values.put(MyDatabaseHelper.TotalHours,totalHours);
database.insert(MyDatabaseHelper.TABLE_WORKDETAILS,空,价值);
database.close();
返回0; }
MyDatabaseHelper.java
公共无效的onCreate(SQLiteDatabase DB)
{
db.execSQL(CREATE TABLE+ TABLE_INFO +(ID INTEGER PRIMARY KEY,Name文本,天气文本,日期DATETIME,状态文本));
db.execSQL(CREATE TABLE+ TABLE_WORKDETAILS +(ID INTEGER PRIMARY KEY,项目文本,WorkDescription TEXT,每文本,TimeIn DATETIME,超时DATETIME,TotalHours DATETIME,TableInfo_id INTEGER,外键(TableInfo_id)参考文献TABLE_INFO(ID)) ;
}
为了使我的问题更容易理解,我总结如下它:
1。这是将数据传递到另一个类,并将它们保存到不同的表中的正确方法是什么?
2.如何插入一个外键到另一个表?
我也问过这关系到我的问题类似的问题。请参阅插入的外键到表
当我跑我的项目,它出来有错误。
4月10日至2日:56:29.104 5016-5030 / com.example.project.project E /表面:getSlotFromBufferLocked:未知缓冲区:0xb4057be0
4月10日至2日:56:29.107 5016-5030 / com.example.project.project D / OpenGLRenderer:手柄0xabe46820上0xa2d54000(RippleDrawable)endAllStagingAnimators
4月10日至2日:56:34.936 5016-5016 / com.example.project.project E / SQLiteLog:(1)近tableWork:语法错误
4月10日至2日:56:34.974 5016-5016 / com.example.project.project D / AndroidRuntime:关闭VM
4月10日至2日:56:34.974 5016-5016 / com.example.project.project E / AndroidRuntime:致命异常:主要
工艺:com.example.project.project,PID:5016
android.database.sqlite.SQLiteException:近tableWork:语法错误(code 1),在编译:创建tableWork详细信息(ID INTEGER PRIMARY KEY,项目文本,WorkDescription TEXT,每文本,TimeIn DATETIME,超时DATETIME ,TotalHours DATETIME,TableInfo_id INTEGER,外键(TableInfo_id)参考文献TABLE_INFO(ID)
在android.database.sqlite.SQLiteConnection.native prepareStatement(本机方法)
在android.database.sqlite.SQLiteConnection.acquire preparedStatement(SQLiteConnection.java:887)
问题是在你创建查询,第二个。
db.execSQL(CREATE TABLE+ TABLE_WORKDETAILS +(ID INTEGER PRIMARY KEY,项目文本,WorkDescription TEXT,每文本,TimeIn DATETIME,超时DATETIME,TotalHours DATETIME,TableInfo_id INTEGER,外键(TableInfo_id)参考文献TABLE_INFO(ID ));
块引用>您记录显示,这是它在执行查询:
android.database.sqlite.SQLiteException:近tableWork:语法错误(code 1),在编译:创建tableWork详细信息(ID INTEGER PRIMARY KEY,项目文本,WorkDescription TEXT,每文本,TimeIn DATETIME ,超时DATETIME,TotalHours DATETIME,TableInfo_id INTEGER,外键(TableInfo_id)参考文献TABLE_INFO(ID)
块引用>首先,你错过了表之后的空间。其次,你不断的
TABLE_WORKDETAILS
值为工作细节
。表名不能有空格,所以将其更改为Work_Details
。你也应该给后空间外键(TableInfo_id)
。这应该是这样的:
db.execSQL(CREATE TABLE+ TABLE_WORKDETAILS +(ID INTEGER PRIMARY KEY,项目文本,WorkDescription TEXT,每文本,TimeIn DATETIME,超时DATETIME,TotalHours DATETIME,TableInfo_id INTEGER,外键(TableInfo_id)参考+ TABLE_INFO +(ID)));
I have two tables,one is
Information
and another isWorkDetailsTable
. The flow of activity isInformation.java>>WorkDetailsTable.java
by intent. Whenbutton
inInformation.java
has clicked, it will save all the data and pass toWorkDetails.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 problem is in your create query, the second one.
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)");
You logs show that this is the query which is executed:
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)
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:
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))");
这篇关于数据传递到另一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!