如何将第一张表ID传递到第二张表以在第二张表中为1名员工插入多条记录
以FIRST_TABLE 1为例,其中包含一些个人详细信息的员工
现在在SECOND_TABLE中员工拥有更多数据,更多行
为了识别我想在Second_table中传递表ID。
谁能帮我
先谢谢了。
这是我的数据库代码
TABLE_NAME_FARMER_SYNC = FIRST_TABLE
TABLE_NAME_ANIMAL_SYNC = SECOND_TABLE
db.execSQL("create table " + TABLE_NAME_FARMER_SYNC + "(NEW_INSURANCE_ID INTEGER PRIMARY KEY AUTOINCREMENT,INSURANCE_ID TEXT,INSURED_NAME TEXT,BANKHYPO_NAME TEXT,FARMER_NAME TEXT,VILLAGE TEXT,TALUKA TEXT,DISTRICT TEXT,TAGGING_DATE DATE)");
db.execSQL("create table " + TABLE_NAME_ANIMAL_SYNC + "(NEW_ANIMAL_ID INTEGER PRIMARY KEY AUTOINCREMENT,FARMER_ID INT,TAG_NO TEXT,ANIMAL_EAR_POSITION TEXT,ANIMAL_SPECIES TEXT,ANIMAL_BREED TEXT,ANIMAL_BODY_COLOR TEXT,ANIMAL_SHAPE_RIGHT TEXT,ANIMAL_SHAPE_LEFT TEXT,ANIMAL_SWITCH_OF_TAIL TEXT,AGE_YEARS NUMBER,ANIMAL_OTHER_MARKS NUMBER,PRAG_STATUS NUMBER,NUMBER_OF_LACTATION NUMBER,CURRENT_MILK NUMBER,SUM_INSURED NUMBER,TAG_IMAGE BLOB NOT NULL,HEAD_IMAGE BLOB NOT NULL,LEFT_SIDE_IMAGE BLOB NOT NULL,RIGHT_SIDE_IMAGE BLOB NOT NULL,TAIL_IMAGE BLOB NOT NULL,IDPROOF_IMAGE BLOB NOT NULL)");
在这里,我想将SECOND_TABEL中的AUTO INCREMENT作为NEW_INSURANCE_ID传递为FARMER_ID。
在FirstTable中插入数据
public boolean addFarmerName(String insurance_id, String insurename, String bankhypo, String farmername, String village, String taluka, String district, String tagging_date) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(INSURANCE_ID, insurance_id);
contentValues.put(INSURED_COLUMN, insurename);
contentValues.put(BNAKHYPO_COLUMN, bankhypo);
contentValues.put(FARMERNAME_COLUMN, farmername);
contentValues.put(VILLAGE_COLUMN, village);
contentValues.put(TALUKA_COLUMN, taluka);
contentValues.put(DISTRICT_COLUMN, district);
contentValues.put(TAGGING_DATE_COLUMN, tagging_date);
db.insert(TABLE_NAME_FARMER_SYNC, null, contentValues);
db.close();
return true;
}
在第二张表中插入数据
public boolean addAnimalsName(String tag, long farmer_id, String ear_position, String animal_species, String animal_breeds, String animal_body_color, String shape_right, String shape_left, String tail, String age, String marks_other, String prag, String lactation, String current_milk, String sum, byte[] tag_image, byte[] head_image, byte[] left_image, byte[] right_image, byte[] tail_image, byte[] id_proof_image) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(TAG_COLUMN, tag);
contentValues.put(FARMER_ID, farmer_id);
contentValues.put(ANIMAL_EAR_POSITION_COLUMN, ear_position);
contentValues.put(ANIMAL_SPECIES_COLUMN, animal_species);
contentValues.put(ANIMAL_BREED_COLUMN, animal_breeds);
contentValues.put(ANIMAL_BODY_COLOR_COLUMN, animal_body_color);
contentValues.put(ANIMAL_SHAPE_RIGHT_COLUMN, shape_right);
contentValues.put(ANIMAL_SHAPE_LEFT_COLUMN, shape_left);
contentValues.put(ANIMAL_SWITCH_OF_TAIL_COLUMN, tail);
contentValues.put(AGE_COLUMN, age);
contentValues.put(ANIMAL_OTHER_MARKS_COLUMN, marks_other);
contentValues.put(PRAG_STATUS_COLUMN, prag);
contentValues.put(NUMBER_OF_LACTATION_COLUMN, lactation);
contentValues.put(CURRENT_MILK_COLUMN, current_milk);
contentValues.put(SUM_INSURED_COLUMN, sum);
contentValues.put(TAG_IMAGE_COLUMN, tag_image);
contentValues.put(HEAD_IMAGE_COLUMN, head_image);
contentValues.put(LEFT_SIDE_IMAGE_COLUMN, left_image);
contentValues.put(RIGHT_SIDE_IMAGE_COLUMN, right_image);
contentValues.put(TAIL_IMAGE_COLUMN, tail_image);
contentValues.put(IDPROOF_IMAGE_COLUMN, id_proof_image);
db.insert(TABLE_NAME_ANIMAL_SYNC, null, contentValues);
db.close();
return true;
}
第一个活动代码
private void addDataInTable() {
boolean insertData = databaseHelper.addFarmerName(id, bank_insured, bank_hypo, editTextFarmerName.getText().toString() + "", editTextVillage.getText().toString() + "", editTextTaluka.getText().toString() + "",
editTextDistrict.getText().toString() + "", editTextTaggingDate.getText().toString() + "");
if (insertData) {
Toast.makeText(context, "Data Save Successfully in SQLite Database", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, "Data Not Save in SQLite Database", Toast.LENGTH_SHORT).show();
}
}
第二个活动代码
private void addDataInTable(byte[] NewEntryImg1, byte[] NewEntryImg2,
byte[] NewEntryImg3, byte[] NewEntryImg4, byte[] NewEntryImg5,
byte[] NewEntryImg6) {
boolean insertData = databaseHelper.addAnimalsName(tag_no, tag1_id, ear_position, animal_species, animal_breed, body_color, shape_right, shape_left,
tail_switch, age, other_marks, prag_status, lactations, milk_qty, sum_insured
, NewEntryImg1, NewEntryImg2, NewEntryImg3, NewEntryImg4, NewEntryImg5, NewEntryImg6);
if (insertData) {
Toast.makeText(context, "Data Save Successfully in SQLite Database", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, "Data Not Save in SQLite Database", Toast.LENGTH_SHORT).show();
}
}
答案 0 :(得分:0)
public int addFarmerName(String insurance_id, String insurename, String bankhypo,
String farmername, String village, String taluka, String district, String
tagging_date) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(INSURANCE_ID, insurance_id);
contentValues.put(INSURED_COLUMN, insurename);
contentValues.put(BNAKHYPO_COLUMN, bankhypo);
contentValues.put(FARMERNAME_COLUMN, farmername);
contentValues.put(VILLAGE_COLUMN, village);
contentValues.put(TALUKA_COLUMN, taluka);
contentValues.put(DISTRICT_COLUMN, district);
contentValues.put(TAGGING_DATE_COLUMN, tagging_date);
db.insert(TABLE_NAME_FARMER_SYNC, null, contentValues);
db.close();
return insurance_id;
}
然后输入第一个活动代码
private void addDataInTable() {
int insuranceId = databaseHelper.addFarmerName(id, bank_insured, bank_hypo, editTextFarmerName.getText().toString() + "", editTextVillage.getText().toString() + "", editTextTaluka.getText().toString() + "",
editTextDistrict.getText().toString() + "", editTextTaggingDate.getText().toString() + "");
if (insuranceId != 0) {
Intent i = new Intent(this, SecondActivity.class);
i.putExtra("id",insuranceId);
startActivityForResult(i, 1);
} else {
Toast.makeText(context, "Data Not Save in SQLite Database", Toast.LENGTH_SHORT).show();
}
}
第二次活动
int id = getIntent().getStringExtra("id");
然后
for(int count =0 ;count<animalList.size();count++){
boolean insertData = databaseHelper.addAnimalsName(tag_no, id, ear_position,
animal_species, animal_breed, body_color, shape_right, shape_left,
tail_switch, age, other_marks, prag_status, lactations, milk_qty,
sum_insured, NewEntryImg1, NewEntryImg2, NewEntryImg3, NewEntryImg4, NewEntryImg5,
NewEntryImg6);
}
if (insertData) {
Toast.makeText(context, "Data Save Successfully in SQLite Database",
Toast.LENGTH_SHORT).show();
Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
setResult(Activity.RESULT_OK,returnIntent);
finish();
} else {
Toast.makeText(context, "Data Not Save in SQLite Database", Toast.LENGTH_SHORT).show();
}
现在处于FirstActivity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == Activity.RESULT_OK){
String result=data.getStringExtra("result");
//success msg
}
if (resultCode == Activity.RESULT_CANCELED) {
//Write your code if there's no result
}
}
} // o