How to pull database from android mobile in MAC? - android

I am developing an android app. I have tested the app in mobile and now i need to see the database of my app(i.e mobile database). I am using Mac and confused how to pull the database from the mobile and see it in SQLite browser.
Thanks for your help guys.

You won't be able to on a real device (unless it is rooted). The data directories are protected against access by anything other than the owning app.
To get it off of the device you need to set up a method in your app to copy the database to somewhere that is accessible (like an SD card). Following is code I've used to do that before:
public void backup() {
try {
File sdcard = Environment.getExternalStorageDirectory();
File outputFile = new File(sdcard,
"yourDB.bak");
if (!outputFile.exists())
outputFile.createNewFile();
File data = Environment.getDataDirectory();
File inputFile = new File(data,
"data/your.package.name/databases/yourDB");
InputStream input = new FileInputStream(inputFile);
OutputStream output = new FileOutputStream(outputFile);
byte[] buffer = new byte[1024];
int length;
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
output.flush();
output.close();
input.close();
} catch (IOException e) {
e.printStackTrace();
throw new Error("Copying Failed");
}
}

Related

Insert external DB in android

I'm doing an application with alot of data in the database so I need to make an external database with SQlite browser and put it in my application
my problem is
I can't see data/data/database because my mobile isn't rooted and I can't root it for reasons
I'm using BlueStack emulator in Eclipse IDE
Please help
Thank you
you can create your database and put it in your assets directory
and on first use copy to data directory.
try {
String destPath = "/data/data/" + getPackageName()
+ "/databases/YOURDbFileName";
File f = new File(destPath);
if(!f.exists()){
Log.v(TAG,"File Not Exist");
InputStream in = getAssets().open("YOURDbFileName");
OutputStream out = new FileOutputStream(destPath);
byte[] buffer = new byte[1024];
int length;
while ((length = in.read(buffer)) > 0) {
out.write(buffer, 0, length);
}
in.close();
out.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
Log.v("TAG","ioexeption");
e.printStackTrace();
}
You need to copy the external database into a folder that is accessible by the phone. So that you can manipulate the database from there.
new location must be: "/data/data/your.app.package/databases/yourdatabasename"

How can I see the contents of a sqlite db of my app in a real device?

I want to see the contents of my database created in my app in the device I deploied it. I can use sqlite commands in shell and see in emulator but not in a real device.
Can anyone know how to do it in real device?
root is not necessary if the application is flagged a debuggable. You can use the run-as adb command.
e.g. to use sqlite :
adb shell "run-as com.your.package sqlite3 databases/database_name"
or copy and pull the database :
adb shell "run-as com.your.package cat databases/database_name > /sdcard/some_name"
adb pull /sdcard/some_name
unfortunately it seems that pre-ICS run-as has quite a few bugs, e.g. issue 20792 or 13965
Actually your Real Device is not Rooted so you don't have a permission to access /data/data/<Application>/database/ directory.
Only one solution copy that file to external Storage .. And see it in SQLiteManager or any other SQLite tools.
I think you need a rooted device to access sqlite.
Look at this discussion.
Try this,
You can copy your database in to your sdcard where you can see your database
public void backup() {
try {
File sdcard = Environment.getExternalStorageDirectory();
File outputFile = new File(sdcard,
"YourDatabase.db");
if (!outputFile.exists())
outputFile.createNewFile();
File data = Environment.getDataDirectory();
File inputFile = new File(data,
"data/"+getPackageName()+"/databases/"+"YourDatabase.db");
InputStream input = new FileInputStream(inputFile);
OutputStream output = new FileOutputStream(outputFile);
byte[] buffer = new byte[1024];
int length;
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
output.flush();
output.close();
input.close();
} catch (IOException e) {
e.printStackTrace();
throw new Error("Copying Failed");
}
}
Hope it helps.
Try this function as
copyFile(new File("data/data/<app>/database"),new File("mnt/sdcard/....")).
public void copyFile(File filesrc, File filedst)
{
try
{
FileInputStream localFileInputStream = new FileInputStream(filesrc);
FileOutputStream localFileOutputStream = new FileOutputStream(filedst);
byte[] arrayOfByte = new byte[1024];
while (true)
{
int i = localFileInputStream.read(arrayOfByte);
if (i == -1)
{
localFileOutputStream.close();
localFileInputStream.close();
break;
}
localFileOutputStream.write(arrayOfByte, 0, i);
}
}
catch (Exception localException)
{
Log.d("XXX", "ExceptioncopyFile:" + localException.getMessage(), localException);
}
}

Save my older application SQLITE based database while installing the newer version in Android?

I made an application in which I save few information in my SQLite database.
As we know that SQLite database resides in the internal memory of the device and its created while app installation and deleted when we remove the respective application.
I am releasing a new version of the application, so when I delete my older version to install the newer version I lost the complete information which I had saved in the older version.
My query is can i retrieve the data from the older version from its SQLite database somehow so that, it can be used and maintained in the newer version.
Please suggest me so that my users must not lost their old data.
If the user updates the application then the database will remain intact. Uninstalling the old version and re-installing the new version will cause the database to be deleted.
See my comment, I really don't think you need to worry about it.
However, if you want to back up and restore your database, the following methods will send it to the SD Card and then read it back in (overwriting what was previously there in each case):
public void backup() {
try {
File sdcard = Environment.getExternalStorageDirectory();
File outputFile = new File(sdcard,
"yourDB.bak");
if (!outputFile.exists())
outputFile.createNewFile();
File data = Environment.getDataDirectory();
File inputFile = new File(data,
"data/your.package.name/databases/yourDB.sqlite");
InputStream input = new FileInputStream(inputFile);
OutputStream output = new FileOutputStream(outputFile);
byte[] buffer = new byte[1024];
int length;
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
output.flush();
output.close();
input.close();
} catch (IOException e) {
e.printStackTrace();
throw new Error("Copying Failed");
}
}
public void restore() {
try {
File sdcard = Environment.getExternalStorageDirectory();
File inputFile = new File(sdcard,
"yourDB.bak");
File data = Environment.getDataDirectory();
File outputFile = new File(data,
"data/your.package.name/databases/yourDB.sqlite");
if (!outputFile.exists())
outputFile.createNewFile();
InputStream input = new FileInputStream(inputFile);
OutputStream output = new FileOutputStream(outputFile);
byte[] buffer = new byte[1024];
int length;
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
output.flush();
output.close();
input.close();
} catch (IOException e) {
e.printStackTrace();
throw new Error("Copying Failed");
}
}

SQLite Database not showing up in File Explorer

I have an SQLite database in my application. When I go to check if the database is created or not in File Explorer, there is no any database file within the data folder. It didn't even show up using the adb shell command. Does this mean that the database hasn't been created yet? Any suggestions on how to solve this?
If you are using an actual device, you will not be able to see or access it from outside unless you root your phone.
If you are using the emulator, the DDMS view will let you navigate to it and pull it from there.
Or, you could have an issue with creating your DB. Without seeing your code we cannot tell.
EDIT
If you want to get the file off of a real device, you'll need to implement a method to copy it from your data directory to someplace where you do have access (such as your SD card). This would do the trick:
public void backup() {
try {
File sdcard = Environment.getExternalStorageDirectory();
File outputFile = new File(sdcard,
"YourDB.bak");
if (!outputFile.exists())
outputFile.createNewFile();
File data = Environment.getDataDirectory();
File inputFile = new File(data, "data/your.package.name/databases/yourDB");
InputStream input = new FileInputStream(inputFile);
OutputStream output = new FileOutputStream(outputFile);
byte[] buffer = new byte[1024];
int length;
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
output.flush();
output.close();
input.close();
} catch (IOException e) {
e.printStackTrace();
throw new Error("Copying Failed");
}
}

Accessible SQLiteDatabase on Android Phone

I'm currently writing a phonebook-type application for the Android OS. I'm currently using a SQLiteDatabase to hold my contact information. I know that the database is held in
data/data/package-name/databases
However, I'd need root access to be able to find that directory on an Android phone. Is there a way to put the database somewhere I can find it without having to root the phone?
Just copy your DB to SD card programatically.
This code is an example how to access your DB =)
try {
input = new FileInputStream("/data/data/my_package_name/databases/" + DATABASE_NAME);
File dir = new File("/sdcard/database_dump");
dir.mkdir();
OutputStream output = new FileOutputStream("/sdcard/debugdump/myDb.db");
byte[] buffer = new byte[1024];
int length;
while ((length = input.read(buffer))>0) {
output.write(buffer, 0, length);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {
e.printStackTrace();
}
After your DB is copied to the sd card you can take it and work with it on your PC using whatever SQLite editor you like. For example: http://www.softpedia.com/get/Internet/Servers/Database-Utils/SqlPro.shtml

Categories

Resources