How to get the path to my Sqlite database?

Asked

Viewed 426 times

2

I need my app to allow me to back up and re-store the data that is recorded in Sqlite. I followed some tutorials from the internet but I’m not able to make a copy of the data!

String packageName = contexto.getPackageName();
String NOME_BD = "estudos_sql.bd";
String BD_FILEPATH = "/data/data/" + packageName + "/databases/" + NOME_BD;

My problem is I can’t find the database file (BD_FILEPATH) even though it is working normally!!!??? Will it be permissions problem?

  • Are you on Windows or Linux (or Freebsd, Mac OS X, ...)? This directory "/data/data/…" is at the root of your system?

  • app is running on Android (emulator or physical device)...

  • I edited the title of the question trying to get it to express its doubt better. If you do not agree with the change click here, to access the revision history, and then the link reverse of the previous revision.

1 answer

4

If your database was created in the traditional way using the class Sqliteopenhelper, can get her way through:

String dbPath = context.getDatabasePath("nome da db").toString();
  • Hello ramaral! I have been analyzing my problem better and this is not in the input file but is in the output file. Always give me Filenotfoundexception error. Here’s my code to see if you can help me:

  • <code>String dbPath = context.getDatabasePath("estudos_sql.bd"). toString(); File dbFile = new File(dbPath); Fileinputstream Fis = new Fileinputstream(dbFile); String outFileName = "backup.db"; File outfile = new File(outFileName); Outputstream output = new Fileoutputstream(outfile); byte[] buffer = new byte[1024]; int length; while ((length = Fis.read(buffer))>0){ output.write(buffer, 0, length); }</code>

  • The error captured through "getMessage()" is: open failed: EROFS (Read-only file system). This indicates that I do not have write permission in the internal memory of Android! What I need to do to free write permission in internal memory?

  • 1

    Alter File outFile = new File(outFileName); for File outFile = new File(context.getFilesDir(), outFileName);

  • ramaral, with this setting works. Now I have another problem that is to find in Android the backup file. If I change the location of the "file. /backup.db" to "/mnt/sdcard/backup.db" get "open failed: ENOENT (No such file or directory)" error. Can help me again?

  • If you want to record in sdcard instead of context.getFilesDir() will need to use getExternal(). Please note that in both cases files are deleted when the app is uninstalled. See all possibilities here

  • Do not code directly(hard code) the file path, always use one of the methods I have indicated to get the path to the directory, then add the file name.

  • Thanks ramaral. If you need more help, I’ll come back again...

  • Ramaral, my main problem is solved. Just one more question: Through the "Environment.getExternal configuration Directory()" the backup file will be created at the root of the Smartphone’s External Storage. However I intended it to be earlier created at the root of external SD card. What configuration do I have to apply?

  • 1

    In fact there is Smartphones which have incorporated a "External Storage" besides the sdcard. Unfortunately the path returned by Environment.getExternalStorageDirectory() refers to the first. To further complicate things the path to the sdcard can vary from brand to brand and even between models of the same brand. I found here a code author claims to function in most Devices.

  • ramaral, thanks again. I will review the code.

Show 6 more comments

Browser other questions tagged

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