0

How can I get the database of my application running on my phone. I use Android Studio and my database is in assets>MyDataBase.db Is there a way to update this file ?

I have my own sqlite database, I insert data then I want to get the file back with the new data.

I don't use the emulator and my database was created by SQLite Browser.

[EDIT]

People said it was a duplicate of this question. But I want to do the same in code line not in command.

Community
  • 1
  • 1
KingMaker
  • 83
  • 1
  • 1
  • 13

3 Answers3

0

Yes you can edit in in many ways. One of them is using libraries such as this. Give it a look and try it in your app.

Drilon Blakqori
  • 2,796
  • 2
  • 17
  • 25
0

If you want to edit file in assets folder of your app in Runtime - this is not possible. you should copy this DB to internal/external storage and edit it after that.

ant
  • 397
  • 1
  • 5
  • 19
0

You cannot write data's to asset/Raw folder, since it is packed(.apk), the assets folder is read-only at runtime.

You need to choose a different storage location, here is a method to backup you db to your sdcard (external storage) dbName=MyDataBase.db in your case:

public static void backupDBonSDcard(Context context, String dbName){
    String DB_PATH = context.getDatabasePath(dbName).getPath();
    Log.d("DB_PATH:" + DB_PATH);
    if(checkDataBase(DB_PATH)){
        InputStream myInput;
        try {
            Log.e("[backupDBonSDcard] saving file to SDCARD");
            myInput = new FileInputStream(DB_PATH);

            // Path to the just created empty db
            String outFileName = Environment.getExternalStorageDirectory().getAbsolutePath() + java.io.File.separator + dbName;

            //Open the empty db as the output stream
            OutputStream myOutput;
            try {
                myOutput = new FileOutputStream(outFileName);
                //transfer bytes from the inputfile to the outputfile
                byte[] buffer = new byte[1024];
                int length;
                while ((length = myInput.read(buffer))>0){
                    myOutput.write(buffer, 0, length);
                }
            } catch (FileNotFoundException | IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                //Close the streams
                if(myOutput!=null){
                    myOutput.flush();
                    myOutput.close();
                }

                if(myInput!=null)
                    myInput.close();
            }
        } catch (FileNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

    }else{
        Log.d("DB "+dbName+" not found");
    }
}

public static boolean checkDataBase(String fileName) {
    java.io.File dbFile = new java.io.File(fileName);
    return dbFile.exists();
}

Don't forget to add the following permission in your manifest:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Gomino
  • 12,127
  • 4
  • 40
  • 49
  • I don't have an external storage. Is it the same procedure ? – KingMaker Jan 12 '16 at 11:16
  • Short answer, yes. Environment.getExternalStorageDirectory() refers to whatever the device manufacturer considered to be "external storage". On some devices, this is removable media, like an SD card. On some devices, this is a portion of on-device flash. Here, "external storage" means "the stuff accessible via USB Mass Storage mode when mounted on a host machine". Don't forget to add the permission `` – Gomino Jan 12 '16 at 11:21
  • checkDataBase(DB_PATH) check if database exists ? – KingMaker Jan 12 '16 at 15:57
  • I updated my answer with the checkDatabase function body :) – Gomino Jan 12 '16 at 21:43
  • Hi @gomino I don't know how I can read the file storage/sdcard0. Do you have any idea ? – KingMaker Jan 18 '16 at 09:30