Preeyah
Preeyah

Reputation: 373

How to join two tables in SQLiteDatabase?

I need to know how to join 2 tables together. I am not sure how to do the joining of tables as I'm new to this.

I've created AnniversaryDBAdapter.class where I create 5 tables in one database. I just need to join 2 tables like join buddiesList table and likes table.

Below is the code of the 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();
}

}

Upvotes: 0

Views: 2877

Answers (2)

Eduardo Teixeira
Eduardo Teixeira

Reputation: 963

Write a query and run with SQLiteDatabase.rawQuery(sql, params).

Upvotes: 0

Jens
Jens

Reputation: 17067

Assuming we correct your table layout to something like this:

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
);

Now, in your setup you can create a VIEW of the JOIN between buddiesList and likes, it will act like a normal table when selecting - you just can't update/delete or insert from it (without messing around with TRIGGERs of course).

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;

A View is created using execSQL - just like a table or trigger.

With a view you can select from a join between buddies and likes, returning all buddies and all their likes, like so:

SELECT * from buddyLikes;

which would return something like this:

_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: If you want foreign-key support in your database you need to call execSQL with:

PRAGMA foreign_keys = ON

in your SQLiteOpenHelper#onOpen(SQLiteDatabase db).

Upvotes: 2

Related Questions