List<User> getAll(); @Query("SELECT * FROM User WHERE id = :id") User getById(String id); @Insert void insert(User user); @Update void update(User user); @Delete void delete(User user); @Query("UPDATE User SET name = :name WHERE id = :id") void updateNameById(long id, String name); }
getAll(): List<User> @Query("SELECT * FROM User WHERE id = :id") fun getById(id: String): User? @Insert fun insert(user: User) @Update fun update(user: User) @Delete fun delete(user: User) @Query("UPDATE User SET name = :name WHERE id = :id") fun updateNameById(id: Long, name: String) }
User") fun getAll(): Flowable<List<User>> @Query("SELECT * FROM User WHERE id = :id") fun getById(id: String): Maybe<User> @Insert fun insert(user: User) @Update fun update(user: User) @Delete fun delete(user: User) @Query("UPDATE User SET name = :name WHERE id = :id") fun updateNameById(id: Long, name: String) }
fun migrate(db: SupportSQLiteDatabase) { db.execSQL(""" CREATE TABLE User2 ( id INTEGER PRIMARY KEY NOT NULL, name TEXT NOT NULL, last_name TEXT NOT NULL ) """.trimIndent()) db.query("SELECT * FROM User").use { cursor -> while (cursor.moveToNext()) { val name = cursor.getString(cursor.getColumnIndex("name")) val values = ContentValues().apply { put("id", cursor.getString(cursor.getColumnIndex("id"))) put("first_name", name.split(" ")[0]) put("last_name", name.split(" ")[1]) } db.insert("User2", OnConflictStrategy.REPLACE, values) } } } }