2017-12-18 199 views
2

我正在嘗試更改我的sqlite數據庫與房間庫。我有點困惑與左連接查詢。android左連接與房間查詢

我已經用sqlite實現了它,但不知道如何才能達到同樣的房間?

這裏是我創建表:

第一個表:通知

db.execSQL("CREATE TABLE IF NOT EXISTS $TABLE_NAME ($COLUMN_ID INTEGER PRIMARY KEY, $ICON TEXT, $TITLE INTEGER," + 
       " $DATE INTEGER, $TYPE INTEGER,$URL TEXT, $MESSAGE INTEGER, FOREIGN KEY($TITLE) REFERENCES ${TableNotificationsTrans.getTableName(this)}(id)," + 
       "FOREIGN KEY($MESSAGE) REFERENCES ${TableNotificationsTrans.getTableName(this)}(id))") 

第二張表:Notification_Trans

db.execSQL("CREATE TABLE IF NOT EXISTS $TABLE_NAME ($COLUMN_ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, $COLUMN_EN TEXT, $COLUMN_GU TEXT, $COLUMN_HI TEXT)") 

我在做什麼是我保存通知中通知表,但其名稱和說明將以特定語言存儲在notification_trans中。

查詢實現

DatabaseHelper.database!!.rawQuery("SELECT A.$COLUMN_ID, A.$ICON, N.${language.toLowerCase()} $TITLE, A.$DATE, A.$TYPE, A.$URL, M.${language.toLowerCase()} $MESSAGE FROM $TABLE_NAME A LEFT JOIN NotificationsTrans N ON A.$TITLE = N.id LEFT JOIN NotificationsTrans M ON A.$MESSAGE = M.id ORDER BY $DATE DESC LIMIT $pageNum*10, 10", null) 

問題

How can I achieve same with room? 

編輯

我的應用程序是多語言的應用程序,在這裏我得到通知標題與特定的語言,l ike印地文或古吉拉特語。我在通知表中存儲通知詳細信息,同時標題notification_trans

NotificationTrans有id,英文,印地文,古吉拉特語的專欄。

當用戶詢問古吉拉特語時,我正在從notificationTrans的列gujarati中重新獲取通知標題。

我可以這樣做,在sqlite中。

但現在我想它

+1

如果您有靜態查詢,您可以在Room中使用它,就像您在SQLite中使用它一樣。在你的情況下,你正在使用字符串插值來生成查詢,在這種情況下Room不能幫你。 – CommonsWare

回答

0

首先你必須做模型類兩種,您可能已經宣告他們,你只需要進行一些更改,如果他們已經存在。

@Entity 
public class Notification { 
    @PrimaryKey 
    int id; 
    String icon; 
    @ForeignKey(entity = Notification_Trans.class, parentColumns = "col_id", childColumns = "id") 
    String title; 
    int date; 
    int type; 
    String url; 
    int msg; 

} 

    @Entity 
public class Notification_Trans { 
    @PrimaryKey(autoGenerate = true) 
    int col_id; 
    String column_en; 
    String column_gu; 
    String column_hi; 

這使得你的POJO,我不明白你的外鍵約束,所以原諒我說,你認爲合適,你可以進行更改。 你可以宣佈你的DAO按查詢this` @Dao

public interface DAO { 

    @Query("SELECT note.id, note.title, note.description, category.name as categoryName " + 
      "FROM note " + 
      "LEFT JOIN category ON note.category_id = category.id") 
    List getCategoryNotes(); 

} 
` 

我還沒有做出改變,我發現在Link here。由於你的查詢是一個複雜的問題,但它會給你一個關於如何做到這一點的想法。, 在此之後,你只需要從數據庫類對象來訪問你的DAO接口,它將處理創造&所有其他東西的房間,像這樣的below`

@Database(entities = {Notification.class, NotificationTrans.class}, version = 3) 
public abstract class AppDatabase extends RoomDatabase { 

    private static AppDatabase instance; 

    public static AppDatabase getAppDatabase(Context context) { 
     if (instance == null) { 
      instance = 
        Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, "database_name") 
          // allow queries on the main thread. 
          // Don't do this on a real app! See PersistenceBasicSample for an example. 
          //.allowMainThreadQueries() 
          .fallbackToDestructiveMigration() 
          .build(); 
     } 
     return instance; 
    } 

    public static void destroyInstance() { 

     instance = null; 
    } 

    public abstract Dao notificationDao(); 

它有助於創建數據庫單獨的類,&跟蹤它的對象。 &你可以用AppDatabase.getAppDatabase(context).notificationDao().yourQueryMethodName();

訪問您的數據,您可能需要參考this理解房間之間的關係,&實現您的要求,

編輯1: 這是你的DAO應該怎麼樣子,`

@Insert 
void insert(Notifications object); 

//這將插入一個項目

@Insert 
void insertAll(Notifications... objects); 

雖然這可以輸入數據的列表,

你可以在這裏調用此方法與數據庫對象,像AppDatabase.getAppDatabase(context).notificationDao().yourQueryMethodName()代替yourQueryMethod(),如果調用insert() &傳給你需要在數據庫中存儲的對象,它會這樣做,

For Eg db.parcelDao().insert(parcel); 這是我如何在我的ParcelDao中插入Data,db是Database對象,& parcel是需要存儲數據的對象。還有一件事,你不能在主線程中調用這個方法,所以你可能需要使用Handler或者AsyncTask來實現這個目的,對不起,我忘了提及這個。 查看房間Training at Android Developers實現房間的基本功能

+0

請給出詳細的答案..我如何插入 –

+0

讓我知道你不明白,我會盡力詳述那部分 –

+0

如何在數據庫中插入值? –