How to access SQLite data from Android in real time (DEBUG)

1

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

My IDE is IntelliJ IDEA and I use the Database connection feature of it to Debug, but sometimes the tool loses communication with the DB and continues returning querys with outdated values (I think IDE saves the Bank locally and query this copy).

Copy the database to a local machine does not answer because I need to debug the changes that my app makes in the DB of the smartphone.

Could you tell me other options for Debug?

    
asked by anonymous 03.05.2016 / 17:09

1 answer

1

Talk to Felipe,

This was a question I always had, I posted in thousands of places and forums and nothing!

What I got was a method that exports the .bd file from SQLite, and with that file I can see it in a program like DB Browser for SQLite for example.

It's a good Lusitano way, but at least for me it works, do the following:

Place this method in your MainActivity, or whatever your application's main class is:

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 bank () method within the OnCreate of your class.

How can you repair 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 you will find the banco.db file on your device, then you can open the file in the DB Browser for SQLite

It's a very complex way to go, but it has worked for me, if you can do something easier, I'll be happy to hear it too.

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

Hugs.

    
03.05.2016 / 21:07