for use room data base in android you need to setup faw things
- first you have to add two deandancy in your gradel file
implementation "android.arch.persistence.room:runtime:1.0.0-beta2"annotationProcessor "android.arch.persistence.room:compiler:1.0.0-beta2"
then make a entity class and dao class in your file structure
Data base class
package com.plzf1.rollearn.database;
import android.arch.persistence.room.Database;
import android.arch.persistence.room.RoomDatabase;
@Database(entities = User.class , version = 1)
public abstract class MyDataBase extends RoomDatabase {
public abstract UserDao userDao();
}
Entity Class
android.arch.persistence.room.ColumnInfo;
import android.arch.persistence.room.Entity;
import android.arch.persistence.room.PrimaryKey;
@Entity(tableName = "user")
public class User
{
@PrimaryKey(autoGenerate = true)
private int userid;
@ColumnInfo(name = "user_name")
private String username;
@ColumnInfo(name = "user_mobile")
private String mobile;
@ColumnInfo(name = "user_balance")
private int userblance;
public int getUserid() {
return userid;
}
public void setUserid(int userid) {
this.userid = userid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public int getUserblance() {
return userblance;
}
public void setUserblance(int userblance) {
this.userblance = userblance;
}
}
User Dao
package com.plzf1.rollearn.database; import android.arch.persistence.room.Dao;import android.arch.persistence.room.Insert;import android.arch.persistence.room.Query;import android.arch.persistence.room.Update; @Daopublic interface UserDao { @Insert void insert(User user); @Update void updateAmout(User user); @Query("SELECT * FROM user WHERE userid=:id") void loadSingle(String id);}
Comments
Post a Comment