@Dao publicinterfaceUserDao{ @Query("SELECT * FROM user") List<User> getAll();
@Query("SELECT * FROM user WHERE uid IN (:userIds)") List<User> loadAllByIds(int[] userIds);
@Query("SELECT * FROM user WHERE first_name LIKE :first AND " + "last_name LIKE :last LIMIT 1") User findByName(String first, String last); @Query("SELECT * FROM user WHERE uid = :userId") UserAddress getUserOrder(int userId); @Insert(onConflict = OnConflictStrategy.REPLACE) voidinsertAddress(Address... addressess);
@Query("SELECT * FROM user WHERE uid IN (:userIds)") List<User> loadAllByIds(int[] userIds);
Query with parameters
有時我們需要給與參數來進行篩選,可以將參數名稱加入指令內,並在前面加冒號:
@Query("SELECT * FROM user WHERE first_name LIKE :first AND " + "last_name LIKE :last LIMIT 1") User findByName(String first, String last);
Query through tables
有時我們也需要由不同的資料表來篩選想要的結果,這時就可以在指令內進行整合資料表的規則:
@Dao publicinterfaceSampleDao{ @Query("SELECT * FROM book " + "INNER JOIN loan ON loan.book_id = book.id " + "INNER JOIN user ON user.id = loan.user_id " + "WHERE user.name LIKE :userName") public List<Book> findBooksBorrowedByNameSync(String userName); }
Query relation data
有時我們需要進行多表查詢,則可以建立一個POJO,並用@Relation設定變數:
publicclassUserAddress{ @Embedded private User user;
@Dao publicinterfaceUserDao{ @Transaction@Query("SELECT * FROM user") List<User> getAll(); @Transaction@Query("SELECT * FROM user WHERE uid = :userId") UserAddress getUserOrder(int userId); }
More about DAO
Returning subsets of columns
給予一個POJO,可以在取得資料的同時,將結果轉成較為精簡的資料類別:
publicclassNameTuple{ @ColumnInfo(name="first_name") public String firstName;
@ColumnInfo(name="last_name") public String lastName; }
然後,函式的回傳值可修改如下:
@Dao publicinterfaceMyDao{ @Query("SELECT first_name, last_name FROM user") public List<NameTuple> loadFullName(); }
@Dao publicinterfaceMyDao{ @Query("SELECT first_name, last_name FROM user WHERE region IN (:regions)") public LiveData<List<User>> loadUsersFromRegionsSync(List<String> regions); }
@Dao publicinterfaceMyDao{ @Query("SELECT * from user where id = :id LIMIT 1") public Flowable<User> loadUserById(int id); }
publicMigrationTest(){ helper = new MigrationTestHelper(InstrumentationRegistry.getInstrumentation(), TargetDb.class.getCanonicalName(), new FrameworkSQLiteOpenHelperFactory()); }
@Test publicvoidmigrate1To2()throws IOException { SupportSQLiteDatabase db = helper.createDatabase(TEST_DB, 1);
// db has schema version 1. insert some data using SQL queries. // You cannot use DAO classes because they expect the latest schema. ContentValues values = new ContentValues(); values.put(<KEY>, <VALUE>); db.insert(<TABLE_NAME>, SQLiteDatabase.CONFLICT_REPLACE, values);
// Prepare for the next version. db.close();
// Re-open the database with version 2 and provide // MIGRATION_1_2 as the migration process. helper.runMigrationsAndValidate(TEST_DB, 2, true, MIGRATION_1_2);
// MigrationTestHelper automatically verifies the schema changes, // but you need to validate that the data was migrated properly. TargetDB targetDb = Room.databaseBuilder(InstrumentationRegistry.getTargetContext(), TargetDb.class, TEST_DB).build(); } }