How to view sqlite database in android application at run time - android

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.

Related

File creation in SD card not working

I am trying to create a file and store it in SD Card to be used as an input for some processing for an apps.
After searching for a while, I got this code which can create a file in SD card.But after running this,I couldn't see any file created in my SD card. Can anyone please help me what I am missing here.
BufferedWriter out = new BufferedWriter(new FileWriter(FileDescriptor.err));
try {
File root = Environment.getExternalStorageDirectory();
if (root.canWrite()) {
File perffile = new File(root, "samplefile.txt");
FileWriter perfwriter = new FileWriter(perffile, true);
out = new BufferedWriter(perfwriter);
}
} catch (IOException e) {
Log.e(TAG, "-Could not write file " + e.getMessage());
return;
}
If you want to add a file or folder or move application into your SD Card just do the following:
steps:
1) Open your Android application's source code file with a text or programming editor.
2) Browse to the location in the source code where you wish to call the function that writes a file to the device's external storage.
3) Insert this single line of code to check for the SD card:
File sdCard = Environment.getExternalStorageDirectory();
4) Insert these lines of code to set the directory and file name:
File dir = new File (sdcard.getAbsolutePath() + "/folder1/folder2");
dir.mkdirs();
File file = new File(dir, "example_file");
// The mkdirs funtion will create the directory folder for you, use it only you want to create a new one.
5) Replace "/folder1/folder2" in the above code with the actual path where you intend to save the file. This should be a location in which you normally save your application files. Also, change the "example_file" value to the actual file name you wish to use.
6) Insert the following line of code to output the file to the SD card:
FileOutputStream f = new FileOutputStream(file);
Finally step 7:
Save the file, then compile it and test the application using the Android emulator software or the device.
This will works!!! ;-)

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 );

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

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();
}
}

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";

Backup /data/system/xxxx.bin to /sdcard

I'm fairly new to apk development. So far, after a book purchase and with a lot of Googling I've managed to make an application that controls some features of my custom ROM. I'm currently trying to implement 2 backup features. I want to backup /data/system/batterystats.bin to /sdcard and also i want to backup launcher.db of my touchwiz launcher to /sdcard.
For the first part i haven't actually found anything. I've searched a lot about how to restore a file, not much has come up. It's mostly about SQL .db files. I've also looked for the possibility to run a shell script via the apk just to perform this backup. With a shell script it's easy work, but doing this via .java, i honestly have no clue.
Also, i've tried quite a lot of code to get my sqlite database file to backup, but i was quite unsuccessful. Here's my code for you to look at:
public class Backup extends Activity {
public void exportDB(){
try {
File sd = Environment.getExternalStorageDirectory();
if (sd.canWrite()) {
String currentDBPath = "data/data/com.sec.android.app.twlauncher/databases/launcher.db";
String backupDBPath = sd + "/launcher.db";
File currentDB = new File(currentDBPath);
File backupDB = new File(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) {
e.printStackTrace();
}
}
}
I have added permissions for external storage write, of course in the androidmanifest, but nothing happens. No FC, it just sits there doing nothing. And when I check my sdcard, there's nothing there.
Any help would be greatly appreciated. Thanks
Download RootTools (the jar file).
You can then run linux commands like this:
RootTools.sendShell(command);
For example to backup, you could do:
RootTools.sendShell("cp -f /data/data/com.sec.android.app.twlauncher/databases/launcher.db /sdcard/directory/");
And to restore the file:
RootTools.sendShell("cp -f /sdcard/directory/launcher.db /data/data/com.sec.android.app.twlauncher/databases/");
cp is the copy command, and -f is what allows it to overwite the file if it already exists.
RootTools are great, and for the commands, just google how to do linux commands and then place them into the sendShell
I have no idea how to do it with java. I personally think that using the linux commands are 10 times easier, though.
And to note, it is actually good to get sdcard location like this:
Environment.getExternalStorageDirectory();
and then append the rest of the storage location info to the end of that.

Categories

Resources