我需要知道如何将2个表连接在一起。我不知道如何加入表格,因为我是新手。
我创建了AnniversaryDBAdapter.class,我在一个数据库中创建了5个表。我只需要加入两个表,如join buddiesList表和喜欢表。
以下是AnniversaryDBAdapter.class的代码
public class AnniversaryDBAdapter
{
private static final String DATABASE_NAME = "AllTables";
private static final int DATABASE_VERSION = 2;
private static final String CREATE_TABLE_BUDDIESLIST = " create table buddiesList(name_id integer primary key autoincrement, name text not null);";
private static final String CREATE_TABLE_LIKES = " create table likes(name_id integer primary key autoincrement,likes text not null);";
private static final String CREATE_TABLE_DISLIKES = " create table dislikes(name_id integer primary key autoincrement, dislikes text not null);";
private static final String CREATE_TABLE_EVENTS = "create table events(date_id integer primary key autoincrement, name_id text not null, date text not null, title_id text not null, starttime text not null, endtime text not null);";
private static final String CREATE_TABLE_TITLE = "create table titles(title_id integer primary key autoincrement, name text not null, image text not null);";
private final Context context;
private static final String TAG = "DBAdapter";
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public AnniversaryDBAdapter(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(CREATE_TABLE_BUDDIESLIST);
db.execSQL(CREATE_TABLE_LIKES);
db.execSQL(CREATE_TABLE_EVENTS);
db.execSQL(CREATE_TABLE_TITLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
Log.w(TAG, "Upgrading database from version "+oldVersion+" to "+newVersion+", which will destroy all old data");
onCreate(db);
}
}
public AnniversaryDBAdapter open() throws SQLException
{
this.db = this.DBHelper.getWritableDatabase();
return this;
}
public void close()
{
this.DBHelper.close();
}
}
答案 0 :(得分:2)
假设我们将您的表格布局更正为:
CREATE TABLE buddiesList(
_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
name TEXT NOT NULL
);
CREATE TABLE likes (
_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
buddy_id INTEGER REFERENCES buddiesList(_id) ON DELETE CASCADE NOT NULL,
likes TEXT NOT NULL
);
现在,在您的设置中,您可以在VIEW
和JOIN
之间创建buddiesList
likes
,在选择时它将像普通表一样 - 您可以不要更新/删除或插入(当然不要乱用TRIGGER)。
CREATE VIEW buddyLikes AS
SELECT buddiesList.*, likes._id AS likes_id, likes.likes as likes
FROM buddiesList LEFT JOIN likes ON buddiesList._id=likes.buddy_id;
使用View
创建execSQL
- 就像表格或触发器一样。
通过视图,您可以选择好友和喜欢之间的联接,返回所有好友及其所有喜欢,如下所示:
SELECT * from buddyLikes;
会返回这样的内容:
_id name likes_id likes
1 |Ted |5 |Facebook
1 |Ted |4 |Murder
2 |Ed |1 |Beer
2 |Ed |2 |Cats
2 |Ed |3 |Stock-car racing
3 |Red |6 |Bananarama
BTW:如果您想在数据库中使用外键支持,则需要致电execSQL
用:
PRAGMA foreign_keys = ON
在SQLiteOpenHelper#onOpen(SQLiteDatabase db)
中。
答案 1 :(得分:0)
编写查询并使用SQLiteDatabase.rawQuery(sql,params)运行。