Backup and restore SQLite database to sdcard
How can I backup my database to the sdcard automatically in my app? And afterward, how do I restore it?
Asked by: Julian372 | Posted: 20-01-2022
Answer 1
How can i backup my database to the sdcard automatically in my app?
Copy it using standard Java I/O. Make sure you don't have any open SQLiteDatabase
objects, though.
And afterwards how do i restore it?
Copy it using standard Java I/O. Make sure you don't have any open SQLiteDatabase
objects to the old database, though.
You can use getPath()
on a SQLiteDatabase
object to find out where it resides, AFAIK (haven't tried this).
Answer 2
Here is my code:
// Local database
InputStream input = new FileInputStream(from);
// create directory for backup
File dir = new File(DB_BACKUP_PATH);
dir.mkdir();
// Path to the external backup
OutputStream output = new FileOutputStream(to);
// transfer bytes from the Input File to the Output File
byte[] buffer = new byte[1024];
int length;
while ((length = input.read(buffer))>0) {
output.write(buffer, 0, length);
}
output.flush();
output.close();
input.close();
Answered by: Daisy395 | Posted: 21-02-2022
Answer 3
Thanks to the existing answers. Here is the complete class (with usage of "getDatabasePath"):
package com.levionsoftware.bills.data.db;
import android.content.Context;
import android.os.Environment;
import android.widget.Toast;
import com.levionsoftware.bills.MyApplication;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;
/**
* Created by denny on 16/05/2016.
* Source: http://stackoverflow.com/questions/18322401/is-it-posible-backup-and-restore-a-database-file-in-android-non-root-devices
*/
public class BackupAndRestore {
public static void importDB(Context context) {
try {
File sd = Environment.getExternalStorageDirectory();
if (sd.canWrite()) {
File backupDB = context.getDatabasePath(DBHandler.getDBName());
String backupDBPath = String.format("%s.bak", DBHandler.getDBName());
File currentDB = new File(sd, backupDBPath);
FileChannel src = new FileInputStream(currentDB).getChannel();
FileChannel dst = new FileOutputStream(backupDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
MyApplication.toastSomething(context, "Import Successful!");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void exportDB(Context context) {
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String backupDBPath = String.format("%s.bak", DBHandler.getDBName());
File currentDB = context.getDatabasePath(DBHandler.getDBName());
File backupDB = new File(sd, backupDBPath);
FileChannel src = new FileInputStream(currentDB).getChannel();
FileChannel dst = new FileOutputStream(backupDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
MyApplication.toastSomething(context, "Backup Successful!");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Answered by: Madaline522 | Posted: 21-02-2022
Similar questions
android - Adding data to a database cursor
Maybe I'm going about this the wrong way, but if so, please correct me.
Here is the situation: I have a query which returns URI strings for ringtones stored in a database.
I am trying to add a "column" to this cursor with the ringer "Title" (since this can change outside my program).
I can successfully use RingtoneManager to get the title, but I cannot figure out how to add this "column" to the...
Update Database Android
Can anyone tell me how to update the database in android. I created an app with an embedded database. I changed the version of the database in the manifest and created the on update method. I wanted to test it to see if the database was updating properly but when I use the adb command only the -r will allow me to do a reinstall but it keeps the database. Is there a way to run the adb command that will allow me to update th...
Select first SMS on Android database inbox
I am desperate to find the solution so I ask for help!
I am a new french programmer. My objective is to create a widget able to show SMS.
My problem is that I don't know how create a cursor which select the first SMS in content://sms/inbox
Excuse my bad English, I hope you will able to understand my wich.
Thank you for your answer.
this is my code:
package sfeir.monwidget;
import android.R.string;
import an...
android - where is database file in the device
iam trying to locate the database files on my nexus device..
is there anyway of doing it without rooting the device?
thanks
ray.
how to get macid and mobile number only in android mobile device not in database
how to get macid and mobile number only in android mobile device not in database
android import export database
there. I am trying to implement a feature into my app to copy the current database unto the sdcard. Are there any tutorials or code snippets to achieve this. I've already looked at this site, http://mgmblog.com/2009/02/06/export-an-android-sqlite-db-to-an-xml-file-on-the-sd-card/ ... but I wont be able t...
Android database backups
I'm looking for ways to back up the database of my app on an Android phone. I know that SQLite databases are just files, so I'd expect to be able to just copy the file to SD-card if one is available.
However, I'm unsure as to how I'd prepare my database/activity for backup/restore.
When starting, my main activity reads the entries from one table in the database and displays them in a ListView. ...
android - How to use my own sqlite database?
I put my database field in "assets" folder. And use the code from this blog to copy the database to "/data/data/my_packname/databases/", (This copy code i run it in the onCreate() method when i run this app) then use select * from ... to get data. But it gives me the exception: no such table.
Some...
android - Use DDMS to push the sqlite database file faild
i did not find my package name folder in the data/data.
There just are many folder named com
And then i try to push my sqlite database to data/data/com, But faild.
Why?
Pls help me .
Can give me a smaple? How to use already exist sqlite database.
I have learned from a blog :
android - Using my own SQLite Database
I have a sqlite database, and i put this file in "assets" folder.
The code like below, Pls help and tell what's wrong in this code,
How to use my own sqlite database.
public class DataBaseHelper extends SQLiteOpenHelper {
private static String DB_PATH = "/data/data/com.SGMalls/databases/";
private static String DB_NAME = "mallMapv2.sqlite";
private SQLiteDatabase myDataB...
android - Adding data to a database cursor
Maybe I'm going about this the wrong way, but if so, please correct me.
Here is the situation: I have a query which returns URI strings for ringtones stored in a database.
I am trying to add a "column" to this cursor with the ringer "Title" (since this can change outside my program).
I can successfully use RingtoneManager to get the title, but I cannot figure out how to add this "column" to the...
Update Database Android
Can anyone tell me how to update the database in android. I created an app with an embedded database. I changed the version of the database in the manifest and created the on update method. I wanted to test it to see if the database was updating properly but when I use the adb command only the -r will allow me to do a reinstall but it keeps the database. Is there a way to run the adb command that will allow me to update th...
android - static database class to use with any activity
I am new to Android, and I haven't developed any app regarding databases on Android yet. So I have a few basic questions. I am after a good database class sample, that will let me to run the CRUD operations. I would like to use it as a static class like:
clsDB->Select("select * from clients");
or
objClient->Delete(clientid);
I am wondering if some...
Loading flat files into Android database table
In Android when you upgrade a database all data is lost.
I'm going through a phase of development right now that is forcing many database upgrades.
I don't want to lose all my data (and have to manually re-enter it) each time I upgrade my database.
I would like to store my data in flat files and load those flat files into their respective tables each time a database upgrade occurs.
Wha...
Select first SMS on Android database inbox
I am desperate to find the solution so I ask for help!
I am a new french programmer. My objective is to create a widget able to show SMS.
My problem is that I don't know how create a cursor which select the first SMS in content://sms/inbox
Excuse my bad English, I hope you will able to understand my wich.
Thank you for your answer.
this is my code:
package sfeir.monwidget;
import android.R.string;
import an...
android - where is database file in the device
iam trying to locate the database files on my nexus device..
is there anyway of doing it without rooting the device?
thanks
ray.
how to get macid and mobile number only in android mobile device not in database
how to get macid and mobile number only in android mobile device not in database
Correct path to sqlite database used by JAR imported into my Android app?
My Android app is using a database through an API packaged in an external JAR. Since the JAR is opening/updating/closing the SQLite database file, do I need to make sure it uses its own package name instead of my own? Im getting errors where it fails to open the database file and Im guessing its because the package name Im telling it to use is incorrect: should it be using the package name in the JAR file?
Code tha...
android import export database
there. I am trying to implement a feature into my app to copy the current database unto the sdcard. Are there any tutorials or code snippets to achieve this. I've already looked at this site, http://mgmblog.com/2009/02/06/export-an-android-sqlite-db-to-an-xml-file-on-the-sd-card/ ... but I wont be able t...
Android database backups
I'm looking for ways to back up the database of my app on an Android phone. I know that SQLite databases are just files, so I'd expect to be able to just copy the file to SD-card if one is available.
However, I'm unsure as to how I'd prepare my database/activity for backup/restore.
When starting, my main activity reads the entries from one table in the database and displays them in a ListView. ...
Still can't find your answer? Check out these communities...
Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android