How to Access Real-Time Android Sqlite Data (DEBUG)

Asked

Viewed 2,683 times

1

How can I access the data contained in the local database of the Android device, ensuring that the information is up to date?

My IDE is Intellij IDEA and I use the connection feature with the database to Debug, but sometimes the tool loses communication with DB and keeps returning querys with outdated values (I think the IDE saves the Bank locally and queries that copy).

Copy the database to a local machine does not suit me because I need to debug the changes my app makes in the smartphone’s DB.

Could inform me other options to perform Debug?

  • http://facebook.github.io/stetho/

1 answer

1

Fala Felipe,

That was a doubt I always had, posted in thousands of places and forums and nothing!

What I got was a method, which exports the file .comic of Sqlite, and with this file I can view it in a program like DB Browser for Sqlite for example.

It’s a very Usitano way, but for me at least it works, do the following:

Put this method into your Mainactivity, or whatever the main class of your application:

private void banco() {

    File f = new File("/data/data/br.com.packagedoseuprojeto/databases/ame.db");
    FileInputStream fis=null;
    FileOutputStream fos=null;

    try
    {
        fis=new FileInputStream(f);
        fos=new FileOutputStream("/mnt/sdcard/db_dump.db");
        while(true)
        {
            int i=fis.read();
            if(i!=-1)
            {fos.write(i);}
            else
            {break;}
        }
        fos.flush();
        Toast.makeText(this, "DB dump OK", Toast.LENGTH_LONG).show();
    }
    catch(Exception e)
    {
        e.printStackTrace();
        Toast.makeText(this, "DB dump ERROR", Toast.LENGTH_LONG).show();
    }
    finally
    {
        try
        {
            fos.close();
            fis.close();
        }
        catch(IOException ioe)
        {}
    }

}

Now call the method bank() within the Oncreate of its class.

How you can notice in the following line:

File("/data/data/br.com.packagedoseuprojeto/databases/banco.db");

You should change the package to the address of your application. This is the path where the file will be db bank. on your device, then you can open the file in the program DB Browser for Sqlite

It’s a very complex way to go, but it’s worked for me, if you get something easier I’ll be happy to know too.

If you have any questions you can send me private text.

Hugs.

  • Um, it doesn’t answer me directly, but it gave me the idea to write Logs with the hints I need. Maybe Junit’s tests can suit me as well. If it works I put here again. Grateful.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.