[英]Android how to access database
My App has a database. 我的应用程序有一个数据库。 Now I would like to copy the database for backup to standard user folder or SD Card.
现在,我想将数据库复制以备份到标准用户文件夹或SD卡。 In Eclipse I find it at data/data/database - but where is the database on the real device?
在Eclipse中,我可以在数据/数据/数据库中找到它-但是实际设备上的数据库在哪里?
The path showed by Eclipse is right, the absolute path change by device, if you have rooted the device you can see the file. Eclipse显示的路径是正确的,绝对路径由设备更改,如果您已将设备植根,则可以看到该文件。 Are always in /data/data/*.
始终位于/ data / data / *中。 If you device is not rooted you cant see this files
如果您的设备未植根,则看不到此文件
在REAL设备中,您无法访问这些文件!!!
try this... just replace lite.db it is name of database of mine. 试试这个...只是替换lite.db,它是我的数据库的名称。
private void copyDB() {
File dir = new File(Environment.getExternalStorageDirectory()
+ "/backup");
if (!dir.exists()) {
dir.mkdirs();
}
File from = new File("/data/data/" + getPackageName() + "/databases/",
"lite.db");
File to = new File(dir, "lite.db");
try {
FileInputStream in = new FileInputStream(from);
FileOutputStream out = new FileOutputStream(to);
FileChannel fromChannel = null, toChannel = null;
try {
fromChannel = in.getChannel();
toChannel = out.getChannel();
fromChannel.transferTo(0, fromChannel.size(), toChannel);
} finally {
if (fromChannel != null)
fromChannel.close();
if (toChannel != null)
toChannel.close();
}
} catch (IOException e) {
Log.e("backup", "Error backuping up database: " + e.getMessage(), e);
}
}
Also not forget to add permission: 同样不要忘记添加权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
The database is stored in the devices data directory, you can get it with Environment.getDataDirectory()
. 该数据库存储在设备数据目录中,您可以使用
Environment.getDataDirectory()
获取它。 In this directory your database is stored under the following path: /data/YOUR.PACKAGE.NAME/databases/YOUR.DB.NAME
. 在此目录中,数据库存储在以下路径下:
/data/YOUR.PACKAGE.NAME/databases/YOUR.DB.NAME
。
Here is a little example how you can backup your database: 这是一个如何备份数据库的小示例:
public void exportDB() {
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if(sd.canWrite()) {
String currentDBPath = "//data//com.example.packagename//databases//" + DB_NAME;
String backupDBPath = DB_NAME;
File currentDB = new File(data, 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();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
Of course, you also need <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
当然,您还需要
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
After that, you can find your database in your SD with the filename given by "DB_NAME". 之后,您可以使用“ DB_NAME”给定的文件名在SD中找到数据库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.