从android拉取Sqlite数据库给出空数据库

问题描述:

我正在开发一个使用 Sqlite 的移动应用程序,我想从手机中提取 .db 文件并在 Sqlite 的数据库浏览器中查看它.

I am developing a mobile application that uses Sqlite and I would like to pull the .db file from the phone and view it in DB Browser for Sqlite.

问题来自这样一个事实,即当我运行 adb 命令时,我得到的 .db 文件是空的,即使我已经创建了表并将数据添加到数据库中的这些表中.

The issue comes from the fact that when I run the adb commands the .db file I get is empty, even though I have creatd tables and added data to those tables in the database.

在应用程序中,我查询数据库中插入的记录并返回它们.有什么想法吗?

In the application I query the database for the inserted records and they are returned. Any thoughts?

adb 命令:adb exec-out run-as projectname cat databases/my.db >my.db

数据库创建代码:

if(!Sqlite.exists("my.db")){
        new Sqlite("my.db")
        .then(
            (db) => {
            //create table here
                db.execSQL("CREATE TABLE IF NOT EXISTS Times (Id INTEGER PRIMARY KEY AUTOINCREMENT, clockIn TEXT, clockOut TEXT)")
                .then(
                    () => {
                        console.log("succcess")
                        db.execSQL("INSERT INTO Times (clockIn, clockOut) VALUES (?, ?)", ["Nic", "Raboy"]).then(() => {
                            db.all("SELECT * FROM Times").then((rows) => {
                                console.log(rows)
                            })
                        })

                    }, 
                    (error) => { 
                        console.log('Error occurred creating table');
                    });
        },
            (error) => {
                console.log("Error creating Database");
            });
    }    

我想出了如何解决这个问题.在 android 9 上,使用 adb 拉取时必须包含 -wal 和 -shm 文件.然后当 .db 文件打开时,您将看到您构建的数据库,而不是一个空的骨架 db.

I figured out how to solve this issue. On android 9 you have to include the -wal and -shm files when pulling using adb. Then when the .db file is opened you will see your database as you have built it and not an empty skeleton db.

这可能不是最好的方法,但它会起作用:

This may not be the best way of doing this but it will work:

adb exec-out run-as projectname cat databases/my.db > my.db
adb exec-out run-as projectname cat databases/my.db-shm > my.db-shm
adb exec-out run-as projectname cat databases/my.db-wal > my.db-wal