What's the proper way to do it? Do I just copy the .sq3 file?
What if there are users on the site and file is being written while it's being copied?
What's the proper way to do it? Do I just copy the .sq3 file?
What if there are users on the site and file is being written while it's being copied?
The sqlite3 command line tool features the .backup
dot command.
You can connect to your database with:
sqlite3 my_database.sq3
and run the backup dot command with:
.backup backup_file.sq3
Instead of the interactive connection to the database, you can also do the backup and close the connection afterwards with
sqlite3 my_database.sq3 ".backup 'backup_file.sq3'"
Either way the result is a copy named backup_file.sq3
of the database my_database.sq3
.
It's different from regularly file copying, because it takes care of any users currently working on the database. There are proper locks set on the database, so the backup is done exclusively.
.backup is the best way.
sqlite3 my_database .backup > my_database.back
you can also try .dump command , it gives you the ability to dump the entire database or tables into a text file. If TABLE specified, only dump tables matching LIKE pattern TABLE.
sqlite3 my_database .dump > my_database.back
try {
final String inFileName = "/data/data/your app package/databases/db";
File dbFile = new File(inFileName);
FileInputStream fis = new FileInputStream(dbFile);
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/CALC/Backup";
File dir = new File(path);
if (!dir.exists()) dir.mkdirs();
String outFileName = path + "/filename"; // output file name
// Open the empty db as the output stream
OutputStream output = new FileOutputStream(outFileName);
// Transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
Toast.makeText(getActivity(), "Backup Successfully", 2).show();
// Close the streams
output.flush();
output.close();
fis.close();
}
catch (Exception e) {
e.printStackTrace();
}