-
-
Notifications
You must be signed in to change notification settings - Fork 871
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #101 from amitshekhariitbhu/development
Add example for room database
- Loading branch information
Showing
9 changed files
with
126 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
app/src/main/java/com/sample/database/room/AppDatabase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.sample.database.room; | ||
|
||
import android.arch.persistence.room.Database; | ||
import android.arch.persistence.room.RoomDatabase; | ||
|
||
/** | ||
* Created by anandgaurav on 12/02/18. | ||
*/ | ||
@Database(entities = {User.class}, version = 1) | ||
public abstract class AppDatabase extends RoomDatabase { | ||
|
||
public abstract UserDao userDao(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.sample.database.room; | ||
|
||
import android.arch.persistence.room.Entity; | ||
import android.arch.persistence.room.PrimaryKey; | ||
|
||
/** | ||
* Created by anandgaurav on 12/02/18. | ||
*/ | ||
@Entity(tableName = "users") | ||
public class User { | ||
|
||
@PrimaryKey | ||
public Long id; | ||
|
||
public String name; | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
app/src/main/java/com/sample/database/room/UserDBHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.sample.database.room; | ||
|
||
import android.arch.persistence.room.Room; | ||
import android.content.Context; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Created by anandgaurav on 12/02/18. | ||
*/ | ||
|
||
public class UserDBHelper { | ||
|
||
private final AppDatabase appDatabase; | ||
|
||
public UserDBHelper(Context context) { | ||
appDatabase = Room.databaseBuilder(context, AppDatabase.class, "User-Database") | ||
.allowMainThreadQueries() | ||
.build(); | ||
} | ||
|
||
public void insertUser(List<User> userList) { | ||
appDatabase.userDao().insertAll(userList); | ||
} | ||
|
||
public int count() { | ||
return appDatabase.userDao().loadAll().size(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.sample.database.room; | ||
|
||
import android.arch.persistence.room.Dao; | ||
import android.arch.persistence.room.Delete; | ||
import android.arch.persistence.room.Insert; | ||
import android.arch.persistence.room.OnConflictStrategy; | ||
import android.arch.persistence.room.Query; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Created by anandgaurav on 12/02/18. | ||
*/ | ||
|
||
@Dao | ||
public interface UserDao { | ||
|
||
@Query("SELECT * FROM users") | ||
List<User> loadAll(); | ||
|
||
@Query("SELECT * FROM users WHERE id IN (:userIds)") | ||
List<User> loadAllByIds(List<Integer> userIds); | ||
|
||
@Insert(onConflict = OnConflictStrategy.REPLACE) | ||
void insert(User user); | ||
|
||
@Insert(onConflict = OnConflictStrategy.REPLACE) | ||
void insertAll(List<User> users); | ||
|
||
@Delete | ||
void delete(User user); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters