samedi 19 mars 2016

Android SQLite - how do I use strftime on 2 joined tables?

In my database, I have 2 SQLite tables and each table has a column called created_at, containing a DATETIME value taken from CURRENT_TIMESTAMP.

The tables are as follows:

    db.execSQL("CREATE TABLE folders (_id INTEGER PRIMARY KEY, " +
            "created_at DATETIME DEFAULT CURRENT_TIMESTAMP, " +
            "parent INTEGER DEFAULT -1, " +
            "hidden INTEGER DEFAULT 0, " +
            "name TEXT);");

    db.execSQL("CREATE TABLE folder_items (" +
            "_id INTEGER PRIMARY KEY, " +
            "created_at DATETIME DEFAULT CURRENT_TIMESTAMP, " +
            "parent_id INTEGER, " +
            "item_id INTEGER, " +
            "item_type INTEGER, " +
            "item_date TEXT" +
            ");");

I want to select data from both tables using an INNER JOIN and a LEFT JOIN.

This is my code so far:

public Cursor getFolderContents(long id, int item_type) {

    SQLiteDatabase db = databaseHelper.getReadableDatabase();

    String args[];

    StringBuilder query = new StringBuilder("SELECT ");
    query.append("pf._id AS pf_id, ");
    query.append("(strftime('%s', pf_created_at) * 1000) AS pf_created_at, ");
    query.append("pf.parent AS pf_parent, ");
    query.append("pf.name AS pf_name, ");

    query.append("f.name AS f_name, ");

    query.append("fi._id AS fi_id, ");
    query.append("(strftime('%s', fi_created_at) * 1000) AS fi_created_at, ");
    query.append("fi.parent_id AS fi_parent_id, ");
    query.append("fi.item_id AS fi_item_id, ");
    query.append("fi.item_type AS fi_item_type, ");
    query.append("fi.item_date AS fi_item_date ");

    query.append("FROM folders pf ");
    query.append("INNER JOIN folder_items fi ON (fi.parent_id = pf._id) ");
    query.append("LEFT JOIN folders f ON (f._id = fi.item_id) ");
    query.append("WHERE pf._id = ? ");

    if (item_type >= 0) {
        query.append("AND fi.item_type = ? ");
        args= new String[]{String.valueOf(id), String.valueOf(item_type)};
    } else {
        args= new String[]{String.valueOf(id)};
    }

    query.append("ORDER BY fi.item_type ASC, LOWER(f.name) ASC, fi.item_date ASC");

    Log.e("getfolder", query.toString());
    return db.rawQuery(query.toString(), args);
}

When the SELECT is executed the following error occurs:

no such column: pf_created_at (code 1): , while compiling: SELECT pf._id AS pf_id, (strftime('%s', pf_created_at) * 1000) AS pf_created_at, pf.parent AS pf_parent, pf.name AS pf_name, f.name AS f_name, fi._id AS fi_id, (strftime('%s', fi_created_at) * 1000) AS fi_created_at, fi.parent_id AS fi_parent_id, fi.item_id AS fi_item_id, fi.item_type AS fi_item_type, fi.item_date AS fi_item_date FROM folders pf INNER JOIN folder_items fi ON (fi.parent_id = pf._id) LEFT JOIN folders f ON (f._id = fi.item_id) WHERE pf._id = ? ORDER BY fi.item_type ASC, LOWER(f.name) ASC, fi.item_date ASC

I assume this occurs at:

query.append("(strftime('%s', pf_created_at) * 1000) AS pf_created_at, ");

How do I write the strftime functions so that they pick the columns from the 2 different tables

Thank you

Aucun commentaire:

Enregistrer un commentaire