SQLite Database not showing up in File Explorer - android

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

Related

Android work with db file from SD card

I wrote an app which logged data and saved it via SQL into a .db File. I had a method copying it from internal memory to SD card.
Now i wrote a second app, which needs to work with this particular .db file. As i think, that apps can't get access to package files from other apps
(in this case
/data/data/app1_package/databases/my_database.db
)
i need somehow to work with my DB on the SD Card. How do i do that?
Can i use this path in my SQLiteHelper class? Should i copy it from SD to my package, is that even possible (access rights etc.)?
I'm a beginner in databases, some help would be nice.
You can open any readable file path as a database:
File dbFile = new File( Environment.getExternalStorageDirectory(), "myfile.db" );
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbFile,null,null);
Note: check if sd-card is mounted before using this code.
yes place the DB file in your assets folder and get it this way :
DB_PATH="/data/data/app1_package/databases/my_database.db"
in your create :
is = getAssets().open("Meaniningss.db");
write(is);
the method :
public void write(InputStream is) {
try {
OutputStream out = new FileOutputStream(new File(DB_PATH));
int read = 0;
byte[] bytes = new byte[1024];
while ((read = is.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
is.close();
out.flush();
out.close();
System.err.println(out + "\n");
} catch (IOException e) {
e.printStackTrace();
}
}

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

How to pull database from android mobile in MAC?

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

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