简体   繁体   English

我无法在数据库 DAO android 中存储值

[英]I can't store values in the database DAO android

Hi I can't store values in the database DAO android.嗨,我无法在数据库 DAO android 中存储值。 I have User.class:我有 User.class:

public class User {
    @PrimaryKey @NonNull 
    public String uid;
@ColumnInfo(name="name")
public String name;
 public User(String uid, String name){
        this.uid=uid;
        this.name=name;
    }
}

DAO database: DAO 数据库:

@Dao
public interface UserDAO {
    @Query("select*from User")
    List<User> getAll();
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insertAll(User users);
}

My Dtababase class is:我的数据库类是:

@Database(entities = {User.class},version=1)
public abstract  class AppDatabase extends RoomDatabase {
    public abstract UserDAO userDAO();
}

My code我的代码

 new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                AppDatabase db = Room.databaseBuilder(activity.getApplicationContext(),
                                        AppDatabase.class, "user_db").build();
                                UserDAO userDao = db.userDAO();
                                userDao.insertAll(new User("11","Melany"));
                                Log.d("main_activity_uid",
                                        "ok  you are pretty");

                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }).start();

Now my code don't store new User(i can't print "ok you are pretty").Why?现在我的代码不存储新用户(我不能打印“好的,你很漂亮”)。为什么? I have another question: i know that when you store data in the database you use threads but what do threads allow you to do?我还有一个问题:我知道当您将数据存储在数据库中时,您使用线程,但是线程允许您做什么?

A problem is that you always create in a new database instance instead of having a singleton database.一个问题是您总是在一个新的数据库实例中创建而不是拥有一个单例数据库。

Single instance ( source ):单实例( 来源):


    private static volatile WordRoomDatabase INSTANCE;

     static WordRoomDatabase getDatabase(final Context context) {
            if (INSTANCE == null) {
                synchronized (WordRoomDatabase.class) {
                    if (INSTANCE == null) {
                        INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
                                WordRoomDatabase.class, "word_database")
                                .build();
                    }
                }
            }
            return INSTANCE;
        }


You need a thread because the Room database does not allow operations on the main thread to prevent blocking the UI ( source )您需要一个线程,因为Room数据库不允许在主线程上进行操作以防止阻塞 UI( 来源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我无法在android studio中输入数据和查询数据库DAO - I cannot enter data and query the database DAO in android studio 我想将多个float值存储到数组中,从mysql数据库中检索到,但是它不存储所有值 - i want to store multiple float values into array,retrieved from mysql database but it doesn't store all values 如何将值(JTextField和JXDatePicker)存储到MySQL数据库中? - How can I store the values(JTextField & JXDatePicker) into MySQL database? 我无法让我的插件在我的 MySQL 数据库中存储值 - I can not get my plugin to store values in my MySQL database 在数据库 DAO 中存储没有重复的对象列表 - Store a list of objects without duplicates in a database DAO 为什么我不能在Android的SQLite数据库中添加这些默认值? - Why can't I add these default values in my SQLite database in Android? 我无法在Java中使用SQLite获得DAO / DTO模式 - I can't get the DAO/DTO pattern with SQLite in java 为什么我不能在ormlite中使用自定义DAO类? - Why i can't use a custom DAO class in ormlite? 如何在Android本地数据库上存储意图或屏幕进度? - How can I store intent or screen progress on android local database? 我无法使用 java netbeans 存储数据本地主机(数据库) - I can't store data localhost(database) using java netbeans
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM