0

我需要知道如何将 2 个表连接在一起。我不知道如何加入表格,因为我是新手。

我创建了 AnniversaryDBAdapter.class,在其中我在一个数据库中创建了 5 个表。我只需要加入 2 个表,比如加入 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();
}

}
4

2 回答 2

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现在,在您的设置中,您可以创建一个JOINbetweenbuddiesListlikes,它在选择时将像一个普通表一样 - 您只是不能更新/删除或插入它(当然不会弄乱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;

AView是使用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

顺便说一句:如果你想在你的数据库中支持外键,你需要调用execSQL

PRAGMA foreign_keys = ON

在你的SQLiteOpenHelper#onOpen(SQLiteDatabase db).

于 2012-08-15T06:09:51.340 回答
0

编写查询并使用 SQLiteDatabase.rawQuery(sql, params) 运行。

于 2013-05-03T18:21:58.790 回答