Android:外键(希望每辆车都有自己的记录保存在SQLite中)
Android: Foreign Key (want every car have its own record to be save in SQLite)
我的android应用程序是关于汽车管理的。
我将有汽车清单及其管理,如(燃料、旅行、维修)。
假设我在列表中有 2 辆车
i) 本田 ABC 123。
ii) 丰田 ABC 298.
我应该怎么做才能将特定汽车的记录存储在table(燃料,行驶,维修)中?
我在燃料、旅行和修理 table 辆汽车 table 中尝试了外键。
但是外键的值为空。
我不知道该怎么办。
这是我的数据库
汽车Table
/*
* ====================================================================
* TABLE CAR COLUMNS
* ====================================================================
*/
public static final String TABLE_CAR = "car";
public static final String COLUMN_CAR_ID = "_id";
public static final String COLUMN_CAR_NAME = "car_name";
* ====================================================================
* CREATING CAR TABLE
* ====================================================================
public static final String DATABASE_CREATE_TABLE_CAR = "create table "
+ TABLE_CAR + "(" + COLUMN_CAR_ID + " integer primary key autoincrement, "
+ COLUMN_CAR_NAME + " text );";
这里是修复table
* ====================================================================
* TABLE REPAIR COLUMNS
* ====================================================================
public static final String TABLE_REPAIR = "repair";
public static final String COLUMN_REPAIR_ID = "_id";
public static final String COLUMN_REPAIR_DATE = "repair_date";
public static final String COLUMN_REPAIR_TYPE = "repair_type";
public static final String COLUMN_REPAIR_TOTAL_COST = "repair_total_cost";
public static final String COLUMN_REPAIR_LOCATION = "repair_location";
public static final String COLUMN_REPAIR_DETAILS = "repair_details";
public static final String COLUMN_REPAIR_CAR_ID = "car_id";
* ====================================================================
* CREATING REPAIR TABLE
* ====================================================================
public static final String DATABASE_CREATE_TABLE_REPAIR = "create table "
+ TABLE_REPAIR + "(" + COLUMN_REPAIR_ID
+ " integer primary key autoincrement, " + COLUMN_REPAIR_DATE
+ " text, " + COLUMN_REPAIR_TYPE + " text not null, "
+ COLUMN_REPAIR_TOTAL_COST + " number, " + COLUMN_REPAIR_DETAILS
+ " text not null, " + COLUMN_REPAIR_LOCATION + " text not null, " + COLUMN_REPAIR_CAR_ID
+ " integer, " +
"FOREIGN KEY (car_id) REFERENCES car(_id)" + " ON DELETE CASCADE );";
这是我的主屏幕,我从那里 select 一辆车
这是我的修复屏幕
这是维修清单(显示两辆车的维修数据,我想要我select)
的车的数据
我得到外键 (car_id) 为空。
你应该使用 ORMLite。使用外键非常容易,您无需编写任何 sql 查询。 link for ormlite. Here is instruction to use (but in Russian)
我的android应用程序是关于汽车管理的。 我将有汽车清单及其管理,如(燃料、旅行、维修)。 假设我在列表中有 2 辆车 i) 本田 ABC 123。 ii) 丰田 ABC 298.
我应该怎么做才能将特定汽车的记录存储在table(燃料,行驶,维修)中? 我在燃料、旅行和修理 table 辆汽车 table 中尝试了外键。 但是外键的值为空。 我不知道该怎么办。
这是我的数据库 汽车Table
/*
* ====================================================================
* TABLE CAR COLUMNS
* ====================================================================
*/
public static final String TABLE_CAR = "car";
public static final String COLUMN_CAR_ID = "_id";
public static final String COLUMN_CAR_NAME = "car_name";
* ====================================================================
* CREATING CAR TABLE
* ====================================================================
public static final String DATABASE_CREATE_TABLE_CAR = "create table "
+ TABLE_CAR + "(" + COLUMN_CAR_ID + " integer primary key autoincrement, "
+ COLUMN_CAR_NAME + " text );";
这里是修复table
* ====================================================================
* TABLE REPAIR COLUMNS
* ====================================================================
public static final String TABLE_REPAIR = "repair";
public static final String COLUMN_REPAIR_ID = "_id";
public static final String COLUMN_REPAIR_DATE = "repair_date";
public static final String COLUMN_REPAIR_TYPE = "repair_type";
public static final String COLUMN_REPAIR_TOTAL_COST = "repair_total_cost";
public static final String COLUMN_REPAIR_LOCATION = "repair_location";
public static final String COLUMN_REPAIR_DETAILS = "repair_details";
public static final String COLUMN_REPAIR_CAR_ID = "car_id";
* ====================================================================
* CREATING REPAIR TABLE
* ====================================================================
public static final String DATABASE_CREATE_TABLE_REPAIR = "create table "
+ TABLE_REPAIR + "(" + COLUMN_REPAIR_ID
+ " integer primary key autoincrement, " + COLUMN_REPAIR_DATE
+ " text, " + COLUMN_REPAIR_TYPE + " text not null, "
+ COLUMN_REPAIR_TOTAL_COST + " number, " + COLUMN_REPAIR_DETAILS
+ " text not null, " + COLUMN_REPAIR_LOCATION + " text not null, " + COLUMN_REPAIR_CAR_ID
+ " integer, " +
"FOREIGN KEY (car_id) REFERENCES car(_id)" + " ON DELETE CASCADE );";
这是我的主屏幕,我从那里 select 一辆车
这是我的修复屏幕
我得到外键 (car_id) 为空。
你应该使用 ORMLite。使用外键非常容易,您无需编写任何 sql 查询。 link for ormlite. Here is instruction to use (but in Russian)