我正在开发一个应用程序,其中我使用sqllite3数据库来存储值。我的Nexus和Nexus7都是无根设备。我如何获取我的应用程序的数据库以进行调试。
我已经尝试了(1)我已经尝试了here提到的所有方法
adb shell
run-as app.package.name \
cp /data/data/package.name/databases/application.sqlite /sdcard/
exit
adb pull /sdcard/application.sqlite ~/
这显示cp未找到..
(2) http://developer.android.com/tools/help/adb.html#sqlite
adb -s emulator-5554 shell
# sqlite3 /data/data/com.example.google.rss.rssexample/databases/rssitems.db
SQLite version 3.3.12
Enter ".help" for instructions
.... enter commands, then quit...
sqlite> .exit
发布于 2013-07-26 22:08:01
您可以使用以下内容将数据库写入外部存储器:
private void writeToSD() throws IOException {
File sd = Environment.getExternalStorageDirectory();
if (sd.canWrite()) {
String currentDBPath = DB_NAME;
String backupDBPath = "backupname.db";
File currentDB = new File(DB_PATH, 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();
}
}
}
其中DB_NAME
是我的数据库的名称,DB_PATH
的定义如下:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
DB_PATH = context.getFilesDir().getAbsolutePath().replace("files", "databases") + File.separator;
}
else {
DB_PATH = context.getFilesDir().getPath() + context.getPackageName() + "/databases/";
}
并添加以下权限(感谢@Sathesh指出这一点):
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
每当我有一个数据库写操作时,我都会调用这个方法,这样我最新的数据库文件就在外部存储器中,我可以从那里查看和调试它。
然后你就可以使用X-Plore app从安卓设备的外部存储器查看数据库了。
发布于 2013-12-27 12:37:13
如果您不知道您的应用程序路径,那么您可以使用以下命令:
public void copyAppDbToExternalStorage() throws IOException {
File sd = Environment.getExternalStorageDirectory();
File currentDB = getApplicationContext().getDatabasePath("databaseName"); //databaseName=your current application database name, for example "my_data.db"
if (sd.canWrite()) {
File backupDB = new File(sd, "toDatabaseName"); // for example "my_data_backup.db"
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();
}
}
}
或者,如果您需要将数据库复制到公共“下载”文件夹,则可以使用以下命令:
public void copyAppDbToDownloadFolder() throws IOException {
File backupDB = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "toDatabaseName"); // for example "my_data_backup.db"
File currentDB = getApplicationContext().getDatabasePath("databaseName"); //databaseName=your current application database name, for example "my_data.db"
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();
}
}
这在我的Nexus4设备上工作得很好。
发布于 2014-05-29 02:21:02
我只是不得不这样做,而且有一种方法可以做到,尽管这是一种痛苦。您需要将文件作为应用程序的用户帐户进行cat,然后通过管道将其传输到可写位置。这对我运行4.3.3的Nexus 4很有效:
adb shell "run-as org.your.application cat /data/data/org.your.application/your-file > /mnt/sdcard/your-file"
adb pull /mnt/sdcard/your-file
https://stackoverflow.com/questions/17883447
复制相似问题