Where to find the android database path?

Asked

Viewed 12,679 times

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 read several tutorials for where the db file created in an Android application goes, however I only think using in Eclipse IDE and not in Android Studio.

Does anyone know where this database file created in the application goes ? I wondered why I wanted to use Sqlite Browser to view my database

inserir a descrição da imagem aqui

2 answers

5

To open this option in Android Studio follow the following 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 must get access to "super user" to be able to view your Bank inside the device. To get this access do as follows:

Navigate to the folder . sdk Platform-tools 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 identifier of the device, done 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 above commands enter the DDMS select again the device and navigate to

data/data/”seu projeto”/databases/banco.db
  • the path of DDMS only has the folder date and nothing else, I did not find the name of my bank there being q it is already created

  • Are you using a physical device for this? if you have you need to free access to the bank, Dac way I explained

  • I am using Bluestack emulator

  • I will edit my reply by placing a print

  • 1

    The answer from @Felipedouradinho, fits perfectly for you so, make a dump to export to SD your BD.

  • just play the code and it’s over ?

Show 1 more comment

4


Is in:

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

Put your seat <nome-do-seu-banco> 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 create a "dump" of 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 know 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" />
  • where could I put the Dump ? right after I created the bank or before?

  • @Gabrielsantanabonatto, the class new File() above does not create a bank, it just arrow the file, so the bank should already exist before moving the dump

  • in that part /data/data/<seu-app>/Databases/<seu-database>. db3 , <your-app> = application name ; <your-database> = database name ; /mnt/sdcard/meu_banco_dump.db = I can leave it that way ?

  • Oops, yeah, just replace <seu-app> by the name of your application (as in manifest.xml and <seu-database> by the (literal) name of your bank.

  • then, I created the bank, but only that this giving "DB dump ERROR" , I tested the two Sqlitedatabase Bancodados = null and the new private String

  • In the debug Can you tell if the file exists? (I updated my reply)

  • java.io.Filenotfoundexception: /data/data/Official/Databases/Nomebanco.db3: open failed: ENOENT (No such file or directory) java.io.Filenotfoundexception: /data/data/Official/Databases/Bancodados.db3: open failed: ENOENT (No such file or directory) (tested with 2)

  • Without putting the extension ". db3" also gives problem? Something else, Oficial is correct? Remember that this should be the package path followed by the application name, e.g..: com.seusite.oficial

  • I removed the db3 extension, the Official think it was incorrect because I put the complete package but still the directory problem

  • After a lot of sacrifice I managed to put in /mnt/sdcard/meu_banco_dump.db , had to put the Nomebanco that was Registration and without the extension . db3... and now how do I open it with Sqlite Browser ?

  • Oops, how did you do? Please tell me to update the answer. The SQLite Browser can be obtained in http://sqlitebrowser.org/

  • File f=new File("/data/data/<seu-app>/Databases/<seu-database>. db3"); take db3, and in <your-database> is the name of the database that is of the String type in the application when creating the database, that is, it is not the variable that you will put, it is the name that you have already put in the application of your database Ex: String Database = "Registration"; File f=new File("/data/date/com.example.gabrielbonatto.oficia/Databases/Cadastro")

  • But now how do I select the file that is there on sdcard inside the emulator?

  • The ideal is to move to the same location of DB! There would already be a new topic hehe

Show 9 more comments

Browser other questions tagged

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