简体   繁体   中英

How to Insert First Table ID in Second Table In SQLite Database in Android?

Here i am inserting Values in 2 Different Table First Table = TABLE_NAME_FARMER_SYNC Second Table = TABLE_NAME_ANIMAL_SYNC

Now in First Table NEW_INSURANCE_ID is PRIMARY KEY AUTO INCREMENT And some other fields And In Second Table NEW_ANIMAL_ID PRIMARY KEY AUTO INCREMENT And also some other fields and Also I want to INSERT NEW_INSURANCE_ID in Second Table

Here In Second Table NEW_INSURANCE_ID is inserting but only First Id Is inserting in every row but i want to clear session and insert new NEW_INSURANCE_ID

Project Concept is : First Table is for Farmer Details Second Table is for Animal Details

So In Second Table Multiple Animals record can be inserted

Here is Database Code

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;
}

First Activity Insert Code

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();
        }

    }

Second Activity Insert Code

 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();
        }

    }

SELECT * FROM table ORDER BY column DESC LIMIT 1;

Get the solution

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM