英文:
Is it save to replace SQLite database
问题
我在我的应用程序中添加了一个选项,用户可以将应用程序备份到Google Drive。
我通过以下方式获取对数据库的引用:
File dbFile = new File(context.getDatabasePath(DATABASE_NAME).getAbsolutePath());
当我记录上述内容以获取路径时,会返回这个路径 - /data/user/0/my.package.name/databases/DatabaseName.DB
然后我创建了数据库的副本并将其上传到Google Drive。
这部分正常运行。
现在,在另一台设备上,我将数据库下载到我的应用程序目录。保存后,我通过以下方式移动/替换旧数据库:
try {
//数据库存储在这里,来自Google Drive
File directoryFile = context.getExternalFilesDir("BackupDB");
InputStream mInput = new FileInputStream(directoryFile + "/DatabaseName.DB");
//当前数据库路径 - /data/user/0/my.package.name/databases/DatabaseName.DB
String outFileName = context.getDatabasePath(DATABASE_NAME).getAbsolutePath();
OutputStream mOutput = new FileOutputStream(outFileName);
byte[] mBuffer = new byte[1024];
int mLength;
while ((mLength = mInput.read(mBuffer)) > 0) {
mOutput.write(mBuffer, 0, mLength);
}
mOutput.flush();
mOutput.close();
mInput.close();
} catch (Exception e) {
e.printStackTrace();
}
上述代码在我的设备上正常工作。
我看到有些人说/data/data/
只能被Root设备访问,但我也看到很多人说我们可以覆盖应用程序的数据库。
我的问题:
我能否在所有Android版本上安全地访问和覆盖我的应用程序数据库?或者是否存在限制?
英文:
I've added an option in my application where the user can backup the application to Google Drive.
I'm getting the reference to the database like this:
File dbFile = new File(context.getDatabasePath(DATABASE_NAME).getAbsolutePath());
When I log the above to get the path, this gets returned - /data/user/0/my.package.name/databases/DatabaseName.DB
I then create a copy of my database and upload it to Google Drive.
This is working fine.
Now, on a different device, I download the database to my application's directory.
After it is saved I move/replace the old database, by doing the following:
try {
//The database is stored here from Google Drive
File directoryFile = context.getExternalFilesDir("BackupDB");
InputStream mInput = new FileInputStream(directoryFile+"/DatabaseName.DB");
//Path to current database - /data/user/0/my.package.name/databases/DatabaseName.DB
String outFileName = context.getDatabasePath(DATABASE_NAME).getAbsolutePath();
OutputStream mOutput = new FileOutputStream(outFileName);
byte[] mBuffer = new byte[1024];
int mLength;
while ((mLength = mInput.read(mBuffer))>0){
mOutput.write(mBuffer, 0, mLength);
}
mOutput.flush();
mOutput.close();
mInput.close();
} catch (Exception e) {
e.printStackTrace();
}
The above works fine on my device.
I've seen some people say that /data/data/
can only be accessed by rooted devices, but I've also seen a lot of people say that we can override our application's database.
My Question:
Is it safe for me to access and override my application database on all android versions? Or are there limitations?
答案1
得分: 0
我认为这没有问题,因为您正在访问对您的应用数据私有的目录,这些目录在应用被删除后会被移除。请查看本页面上的表格:
另一方面,为什么不使用 Cloud Firestore 数据库来替代备份您的 SQLite 数据库,然后将其存储到 Google 驱动器,然后再进行下载等操作呢?
只需在 Firebase Cloud Firestore 上创建您的数据库,并使用规则进行保护,使用 API 将您的数据库加载到任何设备上的应用程序中,并在本地使用 Room 数据库 进行存储,将 Room 作为应用程序的单一数据源。
英文:
I don't think there's a problem with that since you're accessing directories private to you app data and they are removed once the app is removed, check the table on this page:
Data and file storage overview
On the other hand why don't you use Cloud Firestore DB instead of backing your sqlite DB and storing it to google drive then downloading etc..
Just create your DB on FireBase CoudFirestore and secure it with rules, use the API to load your db into the application on any device and store it locally in a Room DB, use Room as your single source of truth for the app.
专注分享java语言的经验与见解,让所有开发者获益!
评论