I'm working on some SQLite code and would like to examine the database. If I run the code on the emulator I am able to pull the file from data\data\myProject\databases using the DDMS file manager but if I run it on actual hardware the data\data folder is inaccessible. Is there any way around thi开发者_开发知识库s other than gaining root access to the phone?
SQLIte Database is actually just a file stored in phone memory, which you can copy to your phone SD card and then easily access it from your PC. Below is the function which does exactly what you need. Just make sure to change your package name and database name before using function.
public static void backupDatabase() throws IOException {
//Open your local db as the input stream
String inFileName = "/data/data/your.package.name/databases/database.sqlite";
File dbFile = new File(inFileName);
FileInputStream fis = new FileInputStream(dbFile);
String outFileName = Environment.getExternalStorageDirectory()
+ "/database.sqlite";
//Open the empty db as the output stream
OutputStream output = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer))>0){
output.write(buffer, 0, length);
}
//Close the streams
output.flush();
output.close();
fis.close();
}
As you may observe from the code, database is always stored in the folder:
/data/data/your.package.name/databases/
Name (in our example "database.sqlite") is the one you picked when extending SQLiteOpenHelper. Obviously, you also need to replace "your.package.name" with the name of your application package.
You have to root your device. Go to command prompt and go to the location where your adb is located.
e.g.
C:\Program Files\Android\android-sdk\platform-tools
type adb root
. now go to your file explorer and check.
Copy file to /mnt/sdcard using
cp /data/data/<packagename>/databases/<dbname>/<filename> /mnt/sdcard
using adb and then copy to your desktop from /mnt/sdcard using
adb pull /mnt/sdcard/<filename>
and then use sqlite3 to view it.
I believe that your application can change the unix permissions of its local storage data files such that they become accessible to other applications and to adb.
However, your application cannot change the lack of list permission on the /data directory of a secured phone.
As a result, you would have to grab the file with 'adb pull /full/path/to/file' - you cannot browse to it since you cannot generate a listing of /data or likely several other intermediate directory parents of your file.
Really about the only way around this is to put your file some place where you CAN get access to it like out on the sdcard during development. Then once you are satisfied things are work correctly put the file where ever you want.
I say this because I have a retail Nexus One and developer Nexus One. While using DDMS File Explorer I noticed I could browse the data folder on the developer device but could not on the retail device. I rooted my retail device and it did not help.
So I guess more research has to be done on this problem. But until then I hope this helps.
精彩评论