Take the 2-minute tour ×

When I export a database on sd card I use

try {
    File sd = new File(Environment.getExternalStorageDirectory()+"/ABC");
                    File data = Environment.getDataDirectory();
                    boolean success = false;
                    if (!sd.exists()) {
                        success = sd.mkdir();
                    if (sd.canWrite()) {
                        String currentDBPath = "\\data\\com.example.skurat\\databases\\Income.db";
                        String backupDBPath = "Income.db";
                        File currentDB = new File(data, currentDBPath);
                        File backupDB = new File(sd, backupDBPath);

                        if (currentDB.exists() && success) {
                            FileChannel src = new FileInputStream(currentDB).getChannel();
                            FileChannel dst = new FileOutputStream(backupDB).getChannel();
                            dst.transferFrom(src, 0, src.size());
                } catch (Exception e) {

as a result file "Income.db" is stored on the sd card.

When I import a new database from sd card I use

File dbfile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +"/ABC/Income.db" ); 
             String backupDBPath = "Income.db";
             File backupDB = new File(dbfile, backupDBPath);
            SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);
            // toDoDBAdapter = new DBAdapter(this, "Income.db"); written in the "onCreate" method, working correctly
            toDoDBAdapter.dbHelper.onUpgrade(db, 1, 2);
            // refresh screen, working correctly

And database is not updated. Why? I use API 7


If you know another way to import the database from sd card in the application, please tell, thanks

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

There is no code that imports the database. Your code just calls onUpgrade on the database on the SD card.

You have to copy the database file back, like you do for export. Alternatively, you have to copy the records from that database into your normal database.

share|improve this answer
add comment

Your code is not clear. Do you actually use any subclass of SQLiteOpenHelper or not? If so the only thing you have to do is to make sure you provide proper target database version in helper constructor and the open helper will react properly running onUpgrade if current version number and target version number do not match.

share|improve this answer
add comment

Your Answer


