Where to find the path of the android database?

6

SQLiteDatabase BancoDados = null;
String NomeBanco = "Cadastro";

CriaBanco();

public void CriaBanco(){
        try{
            BancoDados = openOrCreateDatabase(NomeBanco, MODE_WORLD_READABLE, null);
            String SQL = "CREATE TABLE IF NOT EXISTS tabCadastro11 ( _id INTEGER PRIMARY KEY, nome TEXT, imagePath TEXT ) ";
            BancoDados.execSQL(SQL);
            Criapasta();
            MensagemAlerta("Banco de Dados", "Banco Criado com Sucesso");
        }catch(Exception erro){
            MensagemAlerta("Erro Banco de Dados", "Não foi possivel criar o Banco" + erro);
        }
        finally {
            BancoDados.close();
        }
    }

    public void Criapasta() {
        File f = new File("/data/data/com.example.gabrielbonatto.oficial/databases/Cadastro"); //Já tentei o NomeBanco e o BancoDados
        FileInputStream fis = null;
        FileOutputStream fos = null;

        try {
            fis = new FileInputStream(f);
            fos = new FileOutputStream("/mnt/sdcard/meu_banco_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) {
            }
        }
    }

I have read several tutorials where the db file created in an Android application goes, however I only find it using in the Eclipse IDE and not in Android Studio.

Does anyone know where this database file created in the application goes? I would like to know why I wanted to use SQLite Browser to see my database

    
asked by Gabriel Santana Bonatto 29.06.2015 в 20:56

2 answers

3

You are in:

//data/data/<SEU-APLICATIVO>/databases/<nome-do-seu-banco>

Put your <nome-do-seu-banco> bank out of this folder using a File Manager and rename the extension to .db3 to use in SQLiteExplorer

If you're using an emulator, use DDMS to browse the folders.

If you want to dump your bank using Java on Android

File f=new File("/data/data/<seu-app>/databases/<seu-database>.db3");
FileInputStream fis=null;
FileOutputStream fos=null;

try
{
  fis=new FileInputStream(f);
  fos=new FileOutputStream("/mnt/sdcard/meu_banco_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)
  {}
}

To find out if the database file exists:

File f = new File(...);
if(file.exists())
{      

}

Your manifest.xml must have permission to access the SD card:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    
29.06.2015 / 21:05
5

To open this option in Android Studio follow these steps:

Tools > Android > Android Device Monitor

Inside the path is the same as the traditional eclipse DDMS

/data/data/"seu package"/databases/"seu banco"

Remember that if you are using a physical device as an "emulator" you should get "super user" access to see your Bank inside the device. To obtain this access, do the following:

Navigate to the .sdk \ platform-tools folder by ms-dos and run adb as follows.

.\sdk\platform-tools>adb devices

List of devices attached 0123456789ABCDEF device

This command will list the active devices and give you the device ID, doing this:

adb -s 0123456789ABCDEF shell

Then access via super user:

$ su
# chmod 777 /data
# chmod 777 /data/data
# chmod 777 /data/data/br.com.dominio.projeto
# chmod 777 /data/data/br.com.dominio.projeto/databases
# chmod 777 /data/data/br.com.dominio.projeto/databases/banco.db

After executing the commands above enter the DDMS select the device again and navigate to

data/data/”seu projeto”/databases/banco.db
    
29.06.2015 в 21:06