Pulling Sqlite database from android gives empty database

别说谁变了你拦得住时间么 提交于 2020-07-09 06:49:12

问题


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.

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 command: adb exec-out run-as projectname cat databases/my.db > my.db

db creation code:

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");
            });
    }    

回答1:


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


来源:https://stackoverflow.com/questions/56082256/pulling-sqlite-database-from-android-gives-empty-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!