2

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

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. Commented Apr 14, 2014 at 9: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 Commented Apr 14, 2014 at 9:35
  • stackoverflow.com/a/7227851/3330969 Commented Apr 14, 2014 at 9:43

3 Answers 3

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();
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Are you sure your device is not rooted? I get a file not found exception every time I try your solution
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();
Thank you for the suggestion, I already tried that though, I'm just getting the FileNotFound exception, fail
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);
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.

Comments

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();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.