Hi i am disperate student and i don't understand why i can't add data and query my the database DAO.
@Entity
public class User {
@ColumnInfo(name="name")
public String name;
@ColumnInfo(name="pversion")
public String pversion;
@ColumnInfo(name="upicture")
public byte[] upicture;
@PrimaryKey @NonNull
public String uid;
public User(String uid,
String name,
byte[] upicture, String pversion ){
this.uid=uid;
this.name=name;
this.upicture=upicture;
this.pversion=pversion;
}
public String get_uid(){
return uid;
}
public String get_name(){
return name;
}
public String get_pversion(){
return pversion;
}
public byte[] get_upicture(){
return upicture;
}
}
My Datbase DAO:
@Dao
public interface UserDAO {
@Query("select Exists(select uid from User where uid =:useruid)")
Boolean loadAllByuid(String useruid);
@Query("select*from User")
List<User> getAll();
@Insert
void insertAll(User...users);
}
Appdatabase DAO:
@Database(entities = {User.class},version=1)
public abstract class AppDatabase extends RoomDatabase {
public abstract UserDAO userDAO();
}
In model class:
public class Model_user {
private static Model_user theinstance = null;
private ArrayList<User> user_list;
private static AppDatabase userDAO = null;
public static synchronized Model_user getInstance() {
if (theinstance == null) {
theinstance = new Model_user();
}
return theinstance;
}
public static synchronized Model_user getInstance(Context c) {
if (theinstance == null) {
userDAO = Room.databaseBuilder(c, AppDatabase.class, "userdb").build();
theinstance = new Model_user();
}
return theinstance;
}
public void addUser(User[] s) throws Exception {
Log.d("main_activity_uid","ok sta partendo");
userDAO.userDAO().insertAll(s);
user_list.addAll(Arrays.asList(s));
Log.d("main_activity_uidd","ok hai inserito crrettamente");
return;
}
public Boolean getUsersDBfromuid(String uid) {
return userDAO.userDAO().loadAllByuid(sid);
}
public ArrayList<User> getUsers() {
if (user_list.size() == 0) {
user_list.addAll(userDAO.userDAO().getAll());
}
return user_list;
}
public void getUsersDB() {
user_list.addAll(userDAO.userDAO().getAll());
}
public int getsize() {
return Model_user.getInstance().getUsers().size();
}
Now in my code i cannot enter data and query the database DAO.My code is:
new Thread((new Runnable() {
@Override
public void run() {
try {
Model_user.getInstance
(activity.getApplicationContext()).addUser(user.toArray(new User[0]));
Log.d("main_activitytt",String.valueOf(Model_user.getInstance
(activity.getApplicationContext()).
getUsersDBfromuid("hello")));
} catch (JSONException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
})).start();
why can't I enter data and query the database? where am I wrong? Furthermore, the LOG does not print true or false
Have you checked your code . There is error in your code ?
public Boolean getUsersDBfromuid(String uid) {
return userDAO.userDAO().loadAllByuid(sid);
}
sid shouldbe replaced by uid .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.