如何在Android的SQLite数据库的第二张表中插入第一张表ID?

时间:2019-02-28 06:31:07

标签: android sqlite

在这里我要在2个不同的表中插入值 第一张表= TABLE_NAME_FARMER_SYNC 第二张表= TABLE_NAME_ANIMAL_SYNC

第一个表中的NEW_INSURANCE_ID现在是PRIMARY KEY AUTO INCREMENT和其他一些字段 在第二个表中NEW_ANIMAL_ID主键自动增加,还有其他一些字段,我也想在第二个表中插入NEW_INSURANCE_ID

在第二个表中插入NEW_INSURANCE_ID,但每行仅插入第一个ID,但我想清除会话并插入新的NEW_INSURANCE_ID

项目概念是:  第一个表是关于农民的详细信息 第二张表用于动物详细信息

因此,可以在“第二张表”中插入“多动物”记录

这是数据库代码

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(int farmer_id, String tag, 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(final int i, byte[] NewEntryImg1, byte[] NewEntryImg2,
                                byte[] NewEntryImg3, byte[] NewEntryImg4, byte[] NewEntryImg5,
                                byte[] NewEntryImg6) {
        boolean insertData = databaseHelper.addAnimalsName(datamodel.get(i).getId(), tag_no, 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();
        }

    }

1 个答案:

答案 0 :(得分:0)

SELECT * FROM表ORDER BY列DESC LIMIT 1;

获取解决方案