SQLite Forum

backkup and restore sqlite database between different android phones
Login

backkup and restore sqlite database between different android phones

(1) By anonymous on 2020-09-16 17:32:50 [source]

I have an android application that write to sqlite database. That is installed on two phones(that means it can have different android version / api). One of which has these files: a.db a.db - wal, a.db - shm The other have a.db and a.db - journal. So is it sufficient to backup only a.db and restore form one of which ?

Source = currentDB; dest = backupDB;

if (source.exists()) { FileChannel src = new FileInputStream(source).getChannel(); FileChannel dst = new FileOutputStream(dest).getChannel(); dst.transferFrom(src, 0, src.size()); src.close(); dst.close();

dest.setLastModified(source.lastModified());

}

(2) By Simon Slavin (slavin) on 2020-09-16 23:32:19 in reply to 1 [link] [source]

If your application closes the database correctly, then it should not be leaving a journal file or a - wal file around.

https://sqlite.org/pragma.html#pragma_journal_mode

You appear to have two different phones which are somehow using two different journal modes for their database files. This suggests that something went wrong somewhere and some attention should go into figuring out which mode is best for your application and having all installations switch to that mode. By default, an application uses DELETE mode, so the rest of this is going to be based on the assumption that you're using that mode.

The fact that a journal file still exists means that the application is still running and still has a connection to the database, or that the application crashed while it had a connection to the database.

If you do see a journal file do not delete it. Part of the data that belongs in the database may be in it (simplified for clarity). The correct way to deal with this is to run the application again and do something that makes it use the database file. It will find the journal file and figure out how to handle it. Then quit the application properly and it should delete the journal file as normal.

Under the circumstances, a good answer to your question would be to first check that no journal/wal file exists. This ensures that the database was closed correctly. Only then can you usefully copy that database file around. And since you've already checked that there's no journal/wal file, you don't have to worry about copying it.

(3) By anonymous on 2020-09-17 08:20:26 in reply to 2 [link] [source]

I just have to do a db.close() before I do a backup or restore. That works. Thanks