我的Android应用程序中有这两个表:'account'和'person'。
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("create table account "
+ "(accountId integer primary key autoincrement,bank_name text,hesab_num text unique,cart_num text unique,cvv2 text,expire_date date , pId integer)");
db.execSQL("create table person (personId integer primary key autoincrement,name text)");
}
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
我在android dbHelper类中有这个方法:
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;
}
我调用此方法的大部分时间,帐户表中的外键为空。为什么?我必须做什么?
点:外键可以是重复的,因为一个人可以有很多帐号。