2016-03-19 61 views
0

在我的數據庫中,我有2個SQLite表,每個表都有一個名爲created_at的列,其中包含從CURRENT_TIMESTAMP獲取的DATETIME值。Android SQLite - 如何在2個連接的表上使用strftime?

的表格如下所示:

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

我想選擇使用INNER JOIN從兩個表中的數據和左連接。

這是到目前爲止我的代碼:

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

當選擇執行出現下列錯誤:

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 

我認爲這發生在:

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

怎麼辦我編寫strftime函數,以便他們從兩個不同的表中挑選列

回答

0

發現了問題:

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

應該是:在pf.created_at

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

期間沒有下劃線。