Android 布局此sql lite数据库的正确方法是什么?
当我试图设计自己的项目时,在数据库课上注意力不集中的问题正在困扰着我。我正在尝试使用Android中的数据库 假设我有一个包含两个表的数据库。第一个是这样的:Android 布局此sql lite数据库的正确方法是什么?,android,database,sqlite,Android,Database,Sqlite,当我试图设计自己的项目时,在数据库课上注意力不集中的问题正在困扰着我。我正在尝试使用Android中的数据库 假设我有一个包含两个表的数据库。第一个是这样的: _id(Primary Key) || Type(Foreign Key) || Cost || Size || Date Type(Primary Key) || Size _id是自动递增的,我从不显示它 下一张表如下所示: _id(Primary Key) || Type(Foreign Key) || Cost || Size
_id(Primary Key) || Type(Foreign Key) || Cost || Size || Date
Type(Primary Key) || Size
_id是自动递增的,我从不显示它
下一张表如下所示:
_id(Primary Key) || Type(Foreign Key) || Cost || Size || Date
Type(Primary Key) || Size
第一个表名为DATABASE_table,第二个表名为DATABASE_EXTENDED
现在,当我向第一个表中添加内容时,我也会向第二个表中添加相应的值。当我查询第一个表时,它会正确显示。当我查询第二个表时,它不会显示不同类型的大小选项
示例:如果我添加:
Type || Size
____________
King || Full
King || Half
查询表2时,它将只返回
King || Full
我尝试将大小指定为外键,但它没有任何作用(并抛出错误)
有人能给我指出正确的方向吗
以下是相应的代码片段:
插入所有内容的代码:
public long insertRecord(String type, int cost, String size, String date){
ContentValues values = new ContentValues();
ContentValues others = new ContentValues();
values.put(KEY_TYPE, type);
values.put(KEY_COST, cost);
values.put(KEY_SIZE, size);
values.put(KEY_DATE, date);
others.put(KEY_TYPE, type);
others.put(KEY_SIZE, size);
db.insert(DATABASE_EXTENDED, null, others);
return db.insert(DATABASE_TABLE, null, values);
}
public Cursor getAllTitles() {
return db.query(DATABASE_TABLE, new String[]{
KEY_TYPE,
KEY_COST,
KEY_SIZE,
KEY_DATE},
null,
null,
null,
null,
null);
}
查询所有内容的代码:
public long insertRecord(String type, int cost, String size, String date){
ContentValues values = new ContentValues();
ContentValues others = new ContentValues();
values.put(KEY_TYPE, type);
values.put(KEY_COST, cost);
values.put(KEY_SIZE, size);
values.put(KEY_DATE, date);
others.put(KEY_TYPE, type);
others.put(KEY_SIZE, size);
db.insert(DATABASE_EXTENDED, null, others);
return db.insert(DATABASE_TABLE, null, values);
}
public Cursor getAllTitles() {
return db.query(DATABASE_TABLE, new String[]{
KEY_TYPE,
KEY_COST,
KEY_SIZE,
KEY_DATE},
null,
null,
null,
null,
null);
}
以及查询第二个表的代码:
public Cursor getAllTypes(){
return db.query(DATABASE_EXTENDED, new String[]{
KEY_TYPE,
KEY_SIZE},
null,
null,
null,
null,
null);
}
对于最后一分钟的澄清,以下是一个示例:
type cost size date_installed
King 478 full 2/14/2015
Queen 478 half 2/14/2015
King 478 half 2/14/2015
如果我查询第二个表,它将返回:
type size
King full
Queen half
按照惯例,我一有时间就想好了答案 我为表2制作了一个复合键 那么,现在是:
private static final String DATABASE_CREATE1 =
"create table type_category (type text not null, " +
"size text not null, " +
"primary key (size, type));";
vs
我仍然会遇到一些关于密钥不唯一的错误,但是我想我可以通过一些简单的查询和错误检查来解决这些问题