mercredi 13 janvier 2016

set foreign key in android sqlite

I have these two tables in my android app: 'account' and 'person'

account:
    ACCOUNT_COLUMN_BANK
    ACCOUNT_COLUMN_HESAB
    ACCOUNT_COLUMN_CART
    ACCOUNT_COLUMN_CVV2
    ACCOUNT_COLUMN_EXPIRE_DATE
    ACCOUNT_COLUMN_P_ID     // foreign key for person


person:
    PERSON_COLUMN_NAME

and i have this method in android dbHelper class:

public boolean insertAccount(Person person, Bank bank) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues bankValues = new ContentValues();
    ContentValues personValues = new ContentValues();

    personValues.put(PERSON_COLUMN_NAME, person.getName());
    long id = db.insert(PERSON_TABLE_NAME, null, personValues);  // get id of person

    bankValues.put(ACCOUNT_COLUMN_BANK, bank.getBankName());
    bankValues.put(ACCOUNT_COLUMN_HESAB, bank.getAccountNumber());
    bankValues.put(ACCOUNT_COLUMN_CART, bank.getCardNumber());
    bankValues.put(ACCOUNT_COLUMN_CVV2, bank.getCvv2());
    bankValues.put(ACCOUNT_COLUMN_EXPIRE_DATE, bank.getExpireDate()
            .toString());
    bankValues.put(ACCOUNT_COLUMN_P_ID, (int) id);   // set id of person in account table
    db.insert(ACCOUNT_TABLE_NAME, null, bankValues);

    return true;
}

most of the times that i call this method , foreign key in account table is null. why? and what i must do ?

point: foreign key can be Repetitious , because a person can have many account.

Aucun commentaire:

Enregistrer un commentaire