Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pulling Sqlite database from android gives empty database

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");
            });
    }    
like image 873
cvb Avatar asked Jan 27 '23 08:01

cvb


1 Answers

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
like image 84
cvb Avatar answered Jan 29 '23 10:01

cvb