Java 如何将包含另一个对象的注册表插入数据库

Java 如何将包含另一个对象的注册表插入数据库,java,android,sqlite,Java,Android,Sqlite,在使用外部键的表上插入注册表时遇到问题。为了定义表,我使用类来存储个人信息(姓名、年龄等)。现在,我的一个表使用了一个还包含另一个类的类(外部键来自的原始表)。例如: //Variables declared here private long codeDestiny; private String location; private TypeDestiny typeDestiny; /*Another class I created which has only the primary key

在使用外部键的表上插入注册表时遇到问题。为了定义表,我使用类来存储个人信息(姓名、年龄等)。现在,我的一个表使用了一个还包含另一个类的类(外部键来自的原始表)。例如:

//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();
//将注册表插入表的代码
SQLiteDatabase db=this.getWritableDatabase();
ContentValues=新的ContentValues();
value.put(DESTINY\u COLUMN\u COD,DESTINY.getCodeDestiny());
value.put(TYPEDESTINY\u COLUMN\u TYPE,destiny.getTypeDestiny().getTypeDestiny());
value.put(DESTINY\u COLUMN\u LOCALIDADE,DESTINY.getLocalidade());
long itemId=db.insert(DESTINY\u TABLE\u NAME,null,value);
db.close();
//用于选择所有注册表的代码
List destinies=new ArrayList();
SQLiteDatabase db=this.getReadableDatabase();
String query=“选择*FROM”+
DESTINY_表_名称+”,“+TYPEDESTINY_表_名称+”,其中“+DESTINY_表_名称+”+“+TYPEDESTINY列_类型+”=”+
TYPEDESTINY_表_名称+““+”TYPEDESTINY_列_类型;
Cursor Cursor=db.rawQuery(查询,空);
if(cursor.moveToFirst()){
做{
long codedentine=cursor.getLong(0);
TypeDestiny codTypeDestiny=光标。;
字符串位置=cursor.getString(2);
Destiny Destiny=新的Destiny(codeDestiny,codeTypeDestiny,location);
命运。加上(命运);
}while(cursor.moveToNext());
}
cursor.close();
db.close();