1

I want to upload my sqlite DB file to DropBox. But before that I need to get the sqlite Db file in code. How can I do this on any device? I checked on this link, and it says that the user needs root permissions to access the database.

So in short:

How can I access my sqlite database with non rooted devices? eg File f = new File("path_to_db_file");

If that approach is impossible, how can I save my app database in a place that I will be able to gain access to it?

Please let me know if I can explain anything. Thank you in advance

Community
  • 1
  • 1
user3524019
  • 13
  • 1
  • 3
  • Programmatically create sqlite on SD's folder then you will not require to root your device. If you have already created an sqlite database then if you have limited tables then create a code that will read values from these tables and create a copy of them on another location of SD-card. – Lucifer Apr 14 '14 at 09:23
  • @Kedarnath Thank you for commenting, do you know how to create a database at a specified location? At the moment my DB class is extending SQLiteOpenHelper – user3524019 Apr 14 '14 at 09:35
  • http://stackoverflow.com/a/7227851/3330969 – Lucifer Apr 14 '14 at 09:43

3 Answers3

2

Yes you can, something like this worked for me without root permission:

File Db = new File("/data/data/your.package/databases/yourdatabasename");        
Date d = new Date();         

File file = new File("your_destination.db");
file.setWritable(true);

copyFile(new FileInputStream(Db), new FileOutputStream(file));

Copy file is a function like this, and works in both directions DB->File and File->DB:

public static void copyFile(FileInputStream fromFile, FileOutputStream toFile) throws IOException {
    FileChannel fromChannel = null;
    FileChannel toChannel = null;
    try {
        fromChannel = fromFile.getChannel();
        toChannel = toFile.getChannel();
        fromChannel.transferTo(0, fromChannel.size(), toChannel);
    } finally {
        try {
            if (fromChannel != null) {
                fromChannel.close();
            }
        } finally {
            if (toChannel != null) {
                toChannel.close();
            }
        }
    }
}
Izuel
  • 452
  • 6
  • 14
  • 1
    Are you sure your device is not rooted? I get a file not found exception every time I try your solution – user3524019 Apr 14 '14 at 11:53
  • I'm a lazy guy so I don't have any device rooted for sure because the process sometimes has a lot of steps. Could be that the path to your database is not the correct one? You can get the path with the next line: context.getDatabasePath(DB_NAME).toString(); – Izuel Apr 14 '14 at 12:56
  • 1
    Thank you for the suggestion, I already tried that though, I'm just getting the FileNotFound exception, fail – user3524019 Apr 14 '14 at 14:14
  • FileNotFoundException you are getting because your_destination.db is not present. Add the below code and check File file = new File(your_destination.db); //if file was created earlier the delete the old file if (file.exists()) { file.delete(); } //create new file file.createNewFile(); file.setWritable(true); – Chetan Chaudhari Feb 11 '21 at 05:29
0

I don't know about programmatically but using Rooted Device we can get database from device for that we have to did permission to your app as a Superuser and copy from your app package name and past to your sd card and Access.

0

Working code sample

try {

 File Db = new File("/data/data/your.package/databases/yourdatabasename");        
 Date d = new Date();         

 File file = new File(your_destination.db);

  //if file was created earlier the delete the old file
  if (file.exists()) {
      file.delete();
  }
  
   //create new file
  file.createNewFile();
  file.setWritable(true);

 copyFile(new FileInputStream(Db), new FileOutputStream(file));

}catch (Exception e) {
        e.printStackTrace();
}
Chetan Chaudhari
  • 323
  • 5
  • 15