获取应用SQLite数据库编程文件 [英] Get app sqlite database programmatically in file

查看:163
本文介绍了获取应用SQLite数据库编程文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要上传我的SQLite数据库文件的DropBox。但在此之前,我需要得到code SQLite的数据库文件。我怎样才能做到这一点在任何设备上?我查了<一个href=\"http://stackoverflow.com/questions/4452538/android-location-of-sqlite-database-on-the-device\">this链接,它说,用户需要root权限来访问数据库。

I want to upload my sqlite DB file to DropBox. But before that I need to get the sqlite Db file in code. How can I do this on any device? I checked on this link, and it says that the user needs root permissions to access the database.

因此​​,在短期:

我如何访问我的SQLite数据库与非根深蒂固的设备?例如:文件f =新的文件(path_to_db_file);

How can I access my sqlite database with non rooted devices? eg File f = new File("path_to_db_file");

如果这种做法是不可能的,我怎么能救我的应用程序的数据库中,我将能够获得这些数据的地方吗?

If that approach is impossible, how can I save my app database in a place that I will be able to gain access to it?

请让我知道如果我能解释什么。谢谢你在前进

Please let me know if I can explain anything. Thank you in advance

推荐答案

当然可以,这样的工作对我来说没有root权限:

Yes you can, something like this worked for me without root permission:

File Db = new File("/data/data/your.package/databases/yourdatabasename");        
Date d = new Date();         

File file = new File("your_destination.db");
file.setWritable(true);

copyFile(new FileInputStream(Db), new FileOutputStream(file));

拷贝文件是这样的功能,并在两个方向DB->文件和文件 - > DB作品:

Copy file is a function like this, and works in both directions DB->File and File->DB:

public static void copyFile(FileInputStream fromFile, FileOutputStream toFile) throws IOException {
    FileChannel fromChannel = null;
    FileChannel toChannel = null;
    try {
        fromChannel = fromFile.getChannel();
        toChannel = toFile.getChannel();
        fromChannel.transferTo(0, fromChannel.size(), toChannel);
    } finally {
        try {
            if (fromChannel != null) {
                fromChannel.close();
            }
        } finally {
            if (toChannel != null) {
                toChannel.close();
            }
        }
    }
}

这篇关于获取应用SQLite数据库编程文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆