Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Room-Android Studio数据库检查器不显示数据库_Android_Android Studio_Android Room - Fatal编程技术网

Android Room-Android Studio数据库检查器不显示数据库

Android Room-Android Studio数据库检查器不显示数据库,android,android-studio,android-room,Android,Android Studio,Android Room,我现在正在尝试学习android,我正在构建一个带有数据库的应用程序——我用过房间,但似乎无法让数据库检查器显示我的表格或确认它们存在!我有4个实体和4个DAO。我没有遇到应用程序崩溃,只是在数据库检查器中看不到我的数据库。我试图通过检查者首先检查数据库,然后再以DB为基础完成应用程序的所有LIFEDATA和逻辑。 我构建了android,并交叉检查了我的房间结构和代码实验室。我下载并运行了相同的代码实验室,我可以检查它的数据库,没有问题。我使缓存失效并重新启动 当我在主活动onCreate()

我现在正在尝试学习android,我正在构建一个带有数据库的应用程序——我用过房间,但似乎无法让数据库检查器显示我的表格或确认它们存在!我有4个实体和4个DAO。我没有遇到应用程序崩溃,只是在数据库检查器中看不到我的数据库。我试图通过检查者首先检查数据库,然后再以DB为基础完成应用程序的所有LIFEDATA和逻辑。 我构建了android,并交叉检查了我的房间结构和代码实验室。我下载并运行了相同的代码实验室,我可以检查它的数据库,没有问题。我使缓存失效并重新启动

当我在主活动
onCreate()
中使用“ViewModelProvider”将主活动链接到ViewModel时,它是否绕过了我的构造函数?它似乎不像我从构造函数中获取日志语句一样

当我为房间重构任何东西时,我进入了构建>清理然后构建>重建。   我在main activity(single activity app)>viewmodel>repository>database类中添加了日志语句,并且返回了所有日志,因此代码以正确的顺序调用

当我尝试加载数据库检查器时,我看到以下内容:;

下面是UI“向下”的代码-从主活动开始

public class MainActivity extends AppCompatActivity {

    private ViewModelTimerSetup viewModelTimerSetup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);

        Log.d(ViewModelTimerSetup.getStaticLogTag(), "Main Activity - just before calling viewmodel");
        // generate a setupViewModel and therefore the db (vm > repository > db).
        viewModelTimerSetup = new ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory
                .getInstance(this.getApplication())).get(ViewModelTimerSetup.class);
        Log.d(ViewModelTimerSetup.getStaticLogTag(), "Main Activity - just after calling viewmodel");
视图模型

public class ViewModelTimerSetup extends AndroidViewModel {
    public static String getStaticLogTag() {
        return STATIC_LOG_TAG;
    }
    private static final String STATIC_LOG_TAG = "my_logging";

    private final LiveData<List<TimerDesign>> allTimerDesigns;
    private final LiveData<List<TimerRecords>> allTimerRecords;

    // instantiate the app repository to access the database
    private AppRepository appRepository;

    /**
     * system required constructor, also gets the DBs from the app repository
     * @param application
     */
    public ViewModelTimerSetup(@NonNull Application application) {
        super(application);

        Log.d(STATIC_LOG_TAG, "Log tag - inside ViewModelTimerSetup constructor - pre making new db");
        // instantiate the access to the repository for db access
        appRepository = new AppRepository(application);
        Log.d(STATIC_LOG_TAG, "Log tag - inside the ViewModelTimerSetup constructor - after making new db");
        allTimerDesigns = appRepository.getAllTimerDesigns();
        allTimerRecords = appRepository.getAllTimerRecords();
    }
以下是数据库类(
myDatabaseCallback
为简洁起见省略)

示例实体-所有实体都使用适当的房间注释、getter、setter和1个构造函数构建

@Entity(tableName = "timer_design")
    public class TimerDesign {
    
        @PrimaryKey(autoGenerate = true)
        @ColumnInfo(name = "timer_design_id")
        private int timerDesignID;
    
        @ColumnInfo(name = "timer_name")
        @NonNull
        private String timerName;
    
        @ColumnInfo(name = "total_duration", defaultValue = "00:00:00")
        @NonNull
        private String totalDuration;
    
        /**
         * constructor excl the id (id is autogenerated).
         * @param timerName
         */
        public TimerDesign(String timerName) {
            this.timerName = timerName;
            totalDuration = "00:00:00";
        }
    
        /**
         * setter for the ID
         * @param timerDesignID
         */
        public void setTimerDesignID(int timerDesignID) {
            this.timerDesignID = timerDesignID;
        }
    
        /**
         * getter for the id
         * @return
         */
        public int getTimerDesignID() {
            return timerDesignID;
        }
    
        /**
         * getter for the timer name
         * @return
         */
        public String getTimerName() {
            return timerName;
        }
    
        /**
         * setter for the timer name
         * @param timerName
         */
        public void setTimerName(@NonNull String timerName) {
            this.timerName = timerName;
        }
    
        /**
         * getter for the total duration
         * @return
         */
        @NonNull
        public String getTotalDuration() {
            return totalDuration;
        }
    
        /**
         * setter for the total duration
         * @param totalDuration
         */
        public void setTotalDuration(@NonNull String totalDuration) {
            this.totalDuration = totalDuration;
        }
    }
以及上述实体的DAO

@Dao
public interface TimerDesignDAO {

    // https://developer.android.com/reference/androidx/room/OnConflictStrategy.html
    @Insert(onConflict = OnConflictStrategy.IGNORE)
    void insert(TimerDesign timerDesign);

    @Update
    void update(TimerDesign timerDesign);

    @Delete
    void delete(TimerDesign timerDesign);

    // delete all timers
    @Query("DELETE FROM timer_design")
    void deleteAllTimerDesigns();

    // get a list of all designed timers
    @Query("SELECT * FROM timer_design")
    LiveData<List<TimerDesign>> getAllTimerDesigns();

    // get a single timer design
    @Query("SELECT * FROM timer_design WHERE timer_design_id = :timerID")
    LiveData<List<TimerDesign>> getOneTimerDesign(int timerID);

    // get the id of the most recently inserted timer
    @Query("SELECT MAX(timer_design_id) FROM timer_design")
    int getMostRecentInsertedTimerID();
}

我从设备数据目录下载了DB文件,正如注释中@MikeT所建议的那样。然后我加载到DB Browser中,发现所有4个表都按设计存在,还有几个显然是由OS和Room添加的。在初始数据库创建时请求添加的数据也存在(来自myDatabaseCallback)


在过去的几天里,我的Android Studio升级到了4.2.1,DB inspector正常运行。

数据库中还有什么内容吗?对此不是100%确定,但可能是在您实际添加某些内容之前,数据库不会创建I Ivan,我请求将一些对象添加到RoomDatabase内的一些表中。回调“myDatabaseCallback”,但这对数据库检查器没有影响。同时,我将转到UI,看看一旦构建了完整的UI,我是否可以像用户那样添加对象,然后我就知道这是安卓工作室的问题还是数据库的问题。你可以随时尝试通过设备浏览器查看数据库,它将位于
data/data//databases/
中。你至少希望看到
timers\u database
文件,可能还有timers\u database-wal和timers\u database-shm文件。如果这些存在,那么它与数据库检查器有关。您可以随时复制文件(全部3个)并使用SQLite工具查看它们。嗨,MikeT,好主意!我怎么没想到呢!是的,有一个
timers\u数据库
文件,一个
timers\u数据库
和一个
timers\u数据库
。这三个都有
-rw-rw---
的权限。我会复制它们并按建议检查。我想你的设备至少是安卓8?检查员必须工作。
@Entity(tableName = "timer_design")
    public class TimerDesign {
    
        @PrimaryKey(autoGenerate = true)
        @ColumnInfo(name = "timer_design_id")
        private int timerDesignID;
    
        @ColumnInfo(name = "timer_name")
        @NonNull
        private String timerName;
    
        @ColumnInfo(name = "total_duration", defaultValue = "00:00:00")
        @NonNull
        private String totalDuration;
    
        /**
         * constructor excl the id (id is autogenerated).
         * @param timerName
         */
        public TimerDesign(String timerName) {
            this.timerName = timerName;
            totalDuration = "00:00:00";
        }
    
        /**
         * setter for the ID
         * @param timerDesignID
         */
        public void setTimerDesignID(int timerDesignID) {
            this.timerDesignID = timerDesignID;
        }
    
        /**
         * getter for the id
         * @return
         */
        public int getTimerDesignID() {
            return timerDesignID;
        }
    
        /**
         * getter for the timer name
         * @return
         */
        public String getTimerName() {
            return timerName;
        }
    
        /**
         * setter for the timer name
         * @param timerName
         */
        public void setTimerName(@NonNull String timerName) {
            this.timerName = timerName;
        }
    
        /**
         * getter for the total duration
         * @return
         */
        @NonNull
        public String getTotalDuration() {
            return totalDuration;
        }
    
        /**
         * setter for the total duration
         * @param totalDuration
         */
        public void setTotalDuration(@NonNull String totalDuration) {
            this.totalDuration = totalDuration;
        }
    }
@Dao
public interface TimerDesignDAO {

    // https://developer.android.com/reference/androidx/room/OnConflictStrategy.html
    @Insert(onConflict = OnConflictStrategy.IGNORE)
    void insert(TimerDesign timerDesign);

    @Update
    void update(TimerDesign timerDesign);

    @Delete
    void delete(TimerDesign timerDesign);

    // delete all timers
    @Query("DELETE FROM timer_design")
    void deleteAllTimerDesigns();

    // get a list of all designed timers
    @Query("SELECT * FROM timer_design")
    LiveData<List<TimerDesign>> getAllTimerDesigns();

    // get a single timer design
    @Query("SELECT * FROM timer_design WHERE timer_design_id = :timerID")
    LiveData<List<TimerDesign>> getOneTimerDesign(int timerID);

    // get the id of the most recently inserted timer
    @Query("SELECT MAX(timer_design_id) FROM timer_design")
    int getMostRecentInsertedTimerID();
}
2021-05-12 15:02:37.798 xxx D/my_logging: Main Activity - just before calling viewmodel
2021-05-12 15:02:37.799 xxx D/my_logging: Log tag - inside ViewModelTimerSetup constructor - pre making new db
2021-05-12 15:02:37.803 xxx D/my_logging: Repository - just before calling new database instance
2021-05-12 15:02:37.811 xxx D/my_logging: Database - before building new database instance
2021-05-12 15:02:37.841 xxx D/my_logging: xxx.AllTimersDatabase_Impl@cf67ddc
2021-05-12 15:02:37.841 xxx D/my_logging: Repository - just after calling new database instance
2021-05-12 15:02:37.862 xxx D/my_logging: Log tag - inside the ViewModelTimerSetup constructor - after making new db
2021-05-12 15:02:37.865 xxx D/my_logging: Main Activity - just after calling viewmodel
2021-05-12 15:02:38.349 xxx D/my_logging: Log tag - inside ViewModelTimerSetup constructor - pre making new db
2021-05-12 15:02:38.349 xxx D/my_logging: Repository - just before calling new database instance
2021-05-12 15:02:38.349 xxx D/my_logging: xxx.AllTimersDatabase_Impl@cf67ddc
2021-05-12 15:02:38.350 xxx D/my_logging: Repository - just after calling new database instance
2021-05-12 15:02:38.350 xxx D/my_logging: Log tag - inside the ViewModelTimerSetup constructor - after making new db