2

I need to create a Java class that creates a backup of my database. My problem is, I don't know how to do this using a SQLite database. Can anyone show me how?

1

2 Answers 2

4

If the database is reasonably small, you can just call BEGIN EXCLUSIVE TRANSACTION to prevent other processes from modifying the database, then copy the database file, then ROLLBACK TRANSACTION. If the file is huge, you'll have to use the online backup interface (if your Java bindings don't support it, use JNI).

Of course, if there's just your app using this database and you can be sure that nobody's going to modify the file while you're copying it, just go for it and copy.

Sign up to request clarification or add additional context in comments.

3 Comments

Much better answer than mine- thanks! Didn't know about exclusive transactions either.
But if someone else has already started a transaction I will fail, right?
PRAGMA busy_timeout = milliseconds; can help in such a situation.
0

The easiest thing is to create a copy of the database file- assuming it is a disk-based DB.

1 Comment

Deffinately the easiest and probably best option, since SQLite is a single file database. I had written my won answer but no point keeping it as you beat me to it lol.

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.