How to get Stored SQLite data from internal storage - android

I stored some data into SQLite database, and now i have to get those data from device's internal storage any how, i am using below code but nothing happening when doing tap on button
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
FileChannel source=null;
FileChannel destination=null;
String currentDBPath = "/data/data/app.church.sophie/databases/churchdb.db";
String backupDBPath = "SAMPLE_DB_NAME";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
try {
source = new FileInputStream(currentDB).getChannel();
destination = new FileOutputStream(backupDB).getChannel();
destination.transferFrom(source, 0, source.size());
source.close();
destination.close();
Toast.makeText(ExportDataActivity.this, "DB Exported!", Toast.LENGTH_LONG).show();
} catch(IOException e) {
e.printStackTrace();
}
}

Related

After backing up a database, I can't restore the selected database

I'm having trouble retrieving the .db file from DB card.
I used this code for backup database:
private void DBBackup() {
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String currentDBPath = getContext().getDatabasePath(DBHelper.DB_NAME).getPath();
String backupDBPath = "DBBackupTest";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
if (currentDB.exists()) {
FileChannel src = new FileInputStream(currentDB).getChannel();
FileChannel dst = new FileOutputStream(backupDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
Toast.makeText(getContext(), "Export Successful!", Toast.LENGTH_SHORT).show();
}
}
} catch (Throwable e) {
e.printStackTrace();
}
}
after several minutes, I want to import the backed up database. This is the code for Import
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String currentDBPath = "/storage/emulated/legacy/DBBackup";
String backupDBPath = "DBBackupTest"; // From SD directory.
File backupDB = new File(currentDBPath);
File currentDB = new File(sd, backupDBPath);
FileChannel src = new FileInputStream(backupDB).getChannel();
FileChannel dst = new FileOutputStream(currentDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
Toast.makeText(getContext(), "Import Successful!", Toast.LENGTH_SHORT).show();
}
} catch (Throwable e) {
Toast.makeText(getContext(), "Import Failed!", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
The Toast show "Import Successful!" but when i check the database by using stetho. the database isn't attached. I need your help on this. thanks!
I fixed the problem by changing my currentDB and change it from:
File currentDB = new File(sd, backupDBPath);
to:
File currentDB = new File(getContext().getDatabasePath(DBHelper.DB_NAME).getPath());

unable to see sqlite file on the SD card

my problem is the following:
I have correctly moved the SQlite database on the local SD, but the file is not visible both from the device and the PC via USB connection.
The app does not raise any error.
Here is the method that performs the copy:
private void exportDB(){
DBHelper db = DBHelper.getInstance(this);
String DBName = db.getDBName();
File dbPath = this.getDatabasePath(DBName);
String DBPath = dbPath.getAbsolutePath();
//Toast.makeText(this, "DB Path : " + DBPath , Toast.LENGTH_LONG).show();
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
FileChannel source=null;
FileChannel destination=null;
String currentDBPath = DBPath;
String backupDBPath = "Download/"+DBName;
File currentDB = new File(currentDBPath);
File backupDB = new File(sd, backupDBPath);
try {
source = new FileInputStream(currentDB).getChannel();
destination = new FileOutputStream(backupDB).getChannel();
destination.transferFrom(source, 0, source.size());
source.close();
destination.close();
Toast.makeText(this, "DB Exported!", Toast.LENGTH_LONG).show();
} catch(FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}
}
for transferting DB, I think it may achievable in rooted device.

Android studio copying DB file to internal storage not wroking

I tried to retype see whether any typing error there but it still not working.
It does not even prompt me that I have any errors
I am thinking that is it my internal storage path file given wrongly?
This is where the internal storage file that i wanted to copy to!
This is how i called my method at onclick function:
else if (view == findViewById(R.id.btnBackup)){
exportDatabase("stock.db");
}
}
This is the method for exporting database:
public void exportDatabase(String databaseName) {
try {
File sd = getFilesDir();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String currentDBPath = "//data//"+getPackageName()+"//databases//"+databaseName+"";
String backupDBPath = "//MyDb";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
if (currentDB.exists()) {
FileChannel src = new FileInputStream(currentDB).getChannel();
FileChannel dst = new FileOutputStream(backupDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
}
Toast.makeText(this, "Backup Done!", Toast.LENGTH_SHORT);
}
} catch (Exception e) {
Toast.makeText(this, "Fail!", Toast.LENGTH_SHORT);
}
}
Use this method work perfect.
public void exportDB(Context mContext) {
try {
File sd = mContext.getExternalFilesDir("/database/");
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String currentDBPath = "//data//" + PACKAGE_NAME + "//databases//" + DATABASE_NAME;
String backupDBPath = DATABASE_NAME;
File currentDB = new File(data, currentDBPath);
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();
}
} catch (Exception e) {
}
}

Export database in android

I am exporting my sqllite database using this code.
public void exportDB() {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
FileChannel source = null;
FileChannel destination = null;
String backupDBPath = "contactsManager.db";
String currentDBPath = "/data/" + "com.packagename"
+ "/databases/" + backupDBPath;
System.out.println("oooooooooooooo" + currentDBPath);
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
try {
source = new FileInputStream(currentDB).getChannel();
destination = new FileOutputStream(backupDB).getChannel();
destination.transferFrom(source, 0, source.size());
source.close();
destination.close();
Toast.makeText(getApplicationContext(), "DB Exported!", Toast.LENGTH_LONG)
.show();
System.out.println("---------db exporeted");
} catch (Exception e) {
e.printStackTrace();
}
}
But I am getting a file not found exception.This is the log.
java.io.FileNotFoundException: /data/data/packagename/databases/contactsManager.db: open failed: ENOENT (No such file or directory)
The currentDB (source) does not exist. Check your path.
Also make sure the destinationpath exists with backupDB.mkdir()

How to back up .db files to the SDCard?

Is it possible to copy the .db file such as mmssms.db or user_dict.db to the SDCard?
If it is possible then all I need to do is, read the db file and write it to the SDCard.
You could start with this sample:
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String currentDBPath = "\\data\\{package name}\\databases\\{database name}";
String backupDBPath = "{database name}";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
if (currentDB.exists()) {
FileChannel src = new FileInputStream(currentDB).getChannel();
FileChannel dst = new FileOutputStream(backupDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
}
}
} catch (Exception e) {
// exception
}
// 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();

Categories

Resources