How to download/copy SQLite databse file from Android device to desktop? - android

I have my Android application in which i m using SQLite database. Everything is working fine. But i want to download .db file to check my Database schema and data in database.
But i don't know how to copy SQLite .db file from android device.
i m using Google Nexus 7 for my development.
please help me on that
Thanks in advance

You can use the adb pull command to read a file from the teathered device to your desktop.
E.g. adb pull /data/data/com.foo.bar/databases/MyDatabase

Hi you want o Download SQLite Database Browser Click here to download and then in Eclipse select your project and go to DDMS window there you will find a Sub-window which consists of tabs click on File Explorer and Click on data folder a drop down folder will come and again click on the data folder Now you can see all your projects loaded in eclipse then select your project if it consists Database means it will show you database folder click on that you will find your DB select the DB you want click on Save Icon (Pull a file from device)
and then save your db. Now open the download SQLite db browser and browse the .db extension file you can view you Database .

use this method to copy database to external data storage of device
private void copyDbToExternal(Context context) {
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String currentDBPath = "//data//data//" + context.getApplicationContext().getPackageName() + "//databases//"
+ DB_NAME;
String backupDBPath = DB_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) {
e.printStackTrace();
}
}

Related

How to view sqlite database in android application at run time

I am trying to copy data from DDBMS perspective -> file explorer -> data -> [my_app_package] then PULL this is working when I am running application on emulator but when I am running application on mobile nothing is shown under data folder.
I have also tried to do same thing from adb but its not working.. Is their any way to explore sqllite database created by my application without rooting my phone.
One can surely retreive database .db file from Android Device programmatically. I used to put one more setting under my application named Developer Options which copy .db file into sdCard.
Following code copy .db to sdcard. Change your copied .db file name into whatever like to with backupDBPath and currentDBPath (Name which you gave to your database name).
public void dev()
{
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String currentDBPath = "/data/data/" + getPackageName() + "/databases/ZnameDB";
String backupDBPath = "ZnameDB_Dev.db";
File currentDB = new File(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(SettingsActivity.this, "Database Transfered!", Toast.LENGTH_SHORT).show();
}
}
} catch (Exception e) {
Log.e(TAG, e.toString());
}
}
There are many application available in PlayStore with which you can view your .db file. I used aSQLiteManager android application to view .db file.
No you can't,how ever you can use logs to fetch and see the fetched data in logcat or set some value on your textviews or use table layout too see your fetched data .However the best way is using SQLiteBrowser.You can pull the database file from your emulator or device and then save it on your hard disk and browse it in Sqlitebrowser.
Download SqliteBrowser from here http://sourceforge.net/projects/sqlitebrowser/
You can copy your sqlite file to sdcard programmatically. Follow this link. And view it using any Sqlite Viewer.

Trouble backing up sqlite db to file on certain Android phones. mkdirs() not working sometimes

I have written code to back up my sqlite db to a file on my Android phone (Sony Ericsson X-8 w/ version 2.1.1 of Android).
I have put the code below for reference, but before reading it, I stress that it works fine on the X-8. [Any suggestions for improvement are still most welcome!] It is when I run it on a Sony Ericsson Xperia Arc w/ version 4.0.4 of Android that I have problems.
The file is not written to the SD card. It's very hard to debug, because as you may know, hooking up the phone to the computer via USB card can stop the file from being written anyhow (see for example: Android FileWriter not working on Sony Ericsson Arc but works on another phone).
In any case, going through the code in the debugger results in no problems and one is led to believe the file is written. In my manifest, I do have:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
I am stumped as to what is going on. Did something change between versions 2 and 4 of Android in regards to the file system? Is it phone specific?
Any ideas would be much appreciated.
-Dave
String packageName = "com.xxx.receiver";
String dbName = "Receiver";
//File sd = Environment.getExternalStorageDirectory();
File sd = new File(Environment.getExternalStorageDirectory(), "xxx"); // "xxx" is never created on Arc!
if (!sd.exists()) {
sd.mkdirs();
}
File data = Environment.getDataDirectory();
if (sd.canWrite())
{
String currentDBPath = "//data//"+ packageName +"//databases//"+ dbName;
String backupDBPath = dbName;
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();
//Toast.makeText(SettingsAdapter.this.getContext(), backupDB.toString(), Toast.LENGTH_LONG).show();
}
In Android starting with ICS, USB connections offer the devices as a media device by default, not a USB Mass Storage device. The advantage of that is that external storage doesn't "disappear" when you plug in the USB cable, which of course makes debugging much easier. The disadvantage is that you'll need to let Android know explicitly when you've added a file (or files).
However, it's not terribly hard:
File sd = new File(Environment.getExternalStorageDirectory(), "xxx" );
...
// create your file in xxx
...
sendBroadcast(
new Intent( Intent.ACTION_MEDIA_MOUNTED,
Uri.fromFile( sd ) )
)
);
As a side-note, rather than hard coding the database path, you can use this:
context.getDatabasePath( dbName );

Want to create "databases" folder to store the sqlite database

I have an sqlite database (.db), copied it in assets folder and have to copy it to /data/data/package_name/databases. The problem is I don't have databases folder and did not succeed to create it.
String dirPath = "/data/data/com.gatec.douaa/databases/";
Log.d("Directory",dirPath);
File projDir = new File(dirPath);
if (!projDir.exists())
projDir.mkdirs();
Can you please help me to create that folder to store on it the database?
Thank you very much.
In my experieces, the "databases" directory is created the first time you use the database. So, if you want copy the database from "asset" directory, you have to force the database creation in the path "data/data/package_name/databases".
This happens only in the lollipop (android 5), in my experiences.
Then you can use the code below:
File file = new File(context.getDatabasePath("database_name.db").getPath());
if (!file.exists())
{
try
{
file.createNewFile();
}
catch (IOException e)
{
}
}

Database backup in android

I am using Sql database in my appliaction,in that i want to take backup of the database.I have the following doubts:
1.I am running the application in emulator,for checking whether i have to plug some external storage to check ,if not in my system how can i check.
2.I am using the following code in my application,in that sdcard.write option is showing false,what wrong in this.
Follwing is my code:
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
java.lang.System.out.println("data="+sd.getAbsolutePath());
java.lang.System.out.println("data="+sd.canWrite());--->Showing as false
if (sd.canWrite()) {
String currentDBPath = "\\data\\com.budget\\databases\\budget";
String backupDBPath = "budget";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
java.lang.System.out.println("backup="+backupDB.getAbsolutePath());
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) {
}
Data Backup Documentation in android.
If your Android application has a database and you want your users to be able to backup the database and restore it as they see fit, you’ll need to mess about with database files.
Below is a class called DbExportImport. Once you’ve set it up with the correct values, all you need to do is call exportDb(), importDb() or restoreDb() from your application to perform the necessary operations.
This is also useful as a temporary measure when changing your package name or key for application signing, as your application will be newly installed and you will lose your database.(More).
However, you might want to extend BackupAgent directly if you need to: * Back up data in a database. If you have an SQLite database that you want to restore when the user re-installs your application, you need to build a custom BackupAgent that reads the appropriate data during a backup operation, then create your table and insert the data during a restore operation(More).
Now your can also backup from sdcard Backup and restore SQLite database to sdcard
Here is the another solution just like your problem.
If you are using Eclipse, go to Window - Android Virtual Device Manager, select the AVD you are using and click Edit, in the Hardware options select New and then select the SD Card Support. Then just define the storage size and click Edit AVD, to save the changes.
To check if sd is mounted use:
if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){
//code for sd mounted
}else{
//code for sd not mounted
}
Check if you have setted the permission in the manifest.
<manifest ...
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>
The file extension (.db) is missing in the excerpt above:
currentDBPath = "\\\data\\\com.budget\\\databases\\\budget.db";

How to get sqlite from databases folder in device?

Hello i had implemented this code Using your own SQLite database in Android applications
It takes my db from assets folder and copy it in databases folder into device. Now how could i get that databse from my device ?
or to get is just possible if my device is rooted or even then it's can't be done ?
Thanks.
"currentDBPath" contains the path of the DB. You can copy it as a file and save it some where else using the following steps.
String currentDBPath = "/data/"+getApplicationInfo().packageName+"/databases/db_name";
FileChannel src = new FileInputStream(currentDB).getChannel(); // source
FileChannel dst = new FileOutputStream(backupDB).getChannel(); // destination
dst.transferFrom(src, 0, src.size()); // to copy from source to destination
src.close();
dst.close();
Hope this is what you are looking for.

Categories

Resources