我在使用外部密钥的表上插入注册表时遇到问题。为了定义表,我使用类来存储个人信息(姓名,年龄等)。现在,我的一个表使用了一个类,该类还包含另一个类(它是外部键来自的原始表)。例如:
//Variables declared here
private long codeDestiny;
private String location;
private TypeDestiny typeDestiny; /*Another class I created which has only the primary key (long) and a String that works as a description for the type*/
//One of two constructors
public Destino(TypeDestiny typeDestiny, String location) {
this.typeDestiny= typeDestiny;
this.location = location;
}
//The other constructor
public Destino(long codeDestiny, TypeDestiny typeDestiny, String location) {
this.codDestino = codeDestiny;
this.typeDestiny = typeDestiny;
this.location = location;
}
//Getters
public long getCodeDestiny() {
return codeDestiny;
}
public TypeDestiny getTypeDestiny() {
return typeDestiny;
}
public String getLocation() {
return location;
}
现在,我的问题是我可以使用以下代码创建一个用于插入注册表的方法,但我无法创建一个方法来从同一个表中获取所有注册表。谁能告诉我该怎么做?
// Code for inserting registries into the table
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(DESTINY_COLUMN_COD, destiny.getCodeDestiny());
values.put(TYPEDESTINY_COLUMN_TYPE, destiny.getTypeDestiny().getTypeDestiny());
values.put(DESTINY_COLUMN_LOCALIDADE, destiny.getLocalidade());
long itemId = db.insert(DESTINY_TABLE_NAME, null, values);
db.close();
// Code for selecting all the registries
List<Destiny> destinies = new ArrayList<>();
SQLiteDatabase db = this.getReadableDatabase();
String query = "SELECT * FROM " +
DESTINY_TABLE_NAME + ", " + TYPEDESTINY_TABLE_NAME + " WHERE " + DESTINY_TABLE_NAME + "." + TYPEDESTINY_COLUMN_TYPE + " = " +
TYPEDESTINY_TABLE_NAME + "." + TYPEDESTINY_COLUMN_TYPE ;
Cursor cursor = db.rawQuery(query, null);
if (cursor.moveToFirst()) {
do {
long codeDestiny = cursor.getLong(0);
TypeDestiny codTypeDestiny = cursor.;
String location = cursor.getString(2);
Destiny destiny = new Destiny(codeDestiny, codeTypeDestiny, location);
destinies.add(destiny);
} while (cursor.moveToNext());
}
cursor.close();
db.close();