为什么我无法在文件系统中找到我的SQLite数据库?

时间:2016-06-22 14:19:48

标签: android database sqlite

我对Android中的SQLite数据库实现很陌生,所以我可能忽略了一些东西。我创建了一个SQLite数据库,其类扩展为SQLiteOpenHelper

public class DB extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "/mnt/sdcard/db/db.sqlite";
    private static final int DATABASE_VERSION = 1;
    private static final String TEXT_TYPE = " TEXT";
    private static final String COMMA_SEP = ",";

    public static final String TABLE_EXTRA_DATA = "extra_data";
    public static final String KEY_ID = "id";
    public static final String KEY_LINKED_ORDER_NUMBER = "linked_order_number";
    public static final String KEY_DATE = "date";
    public static final String KEY_ARRIVAL_TIME = "arrival_time";
    public static final String KEY_ARRIVAL_IMPORTANT = "arrival_important";
    public static final String KEY_CLIENT_NAME = "client_name";

    public static final String CREATE_TABLE_EXTRA_DATA = "CREATE TABLE IF NOT EXISTS " +
            TABLE_EXTRA_DATA + " (" +
            KEY_ID + " INTEGER PRIMARY KEY," +
            KEY_LINKED_ORDER_NUMBER + TEXT_TYPE + COMMA_SEP +
            KEY_DATE + TEXT_TYPE + COMMA_SEP +
            KEY_ARRIVAL_TIME + TEXT_TYPE + COMMA_SEP +
            KEY_ARRIVAL_IMPORTANT + TEXT_TYPE + COMMA_SEP +
            KEY_CLIENT_NAME + TEXT_TYPE + " )";

    public static final String DELETE_TABLE = "DROP TABLE IF EXISTS " + TABLE_EXTRA_DATA;

    private final Activity _context;

    public DB(Activity context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        _context = context;
    }

    @Override
    public void onCreate(SQLiteDatabase database) {
        database.execSQL(CREATE_TABLE_EXTRA_DATA);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL(DELETE_TABLE);
        onCreate(db);
    }

    @Override
    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        onUpgrade(db, oldVersion, newVersion);
    }
}

我有一个数据库助手类来使用数据库上的操作:

public class DbHelper {
    private DB _dbHelper;
    private SQLiteDatabase _db;

    public DbHelper(Activity context)
    {
        _dbHelper = new DB(context);
        _db = _dbHelper.getWritableDatabase();
    }

    public void CloseDb() {
        if (_db != null)
        {
            if (_db.isOpen())
                _db.close();
        }
    }

    public void SaveExtraData() {
        ContentValues cv = new ContentValues();
        cv.put(_dbHelper.KEY_ID,"test");
        cv.put(_dbHelper.KEY_DATE,"test");
        cv.put(_dbHelper.KEY_ARRIVAL_TIME,"test");
        cv.put(_dbHelper.KEY_ARRIVAL_IMPORTANT,"test");
        cv.put(_dbHelper.KEY_CLIENT_NAME,"test");

        _db.insert(_dbHelper.TABLE_EXTRA_DATA, null, cv);
    }
}

在活动中启动数据库和databaseHelper并在数据库中添加一些数据时,我没有遇到任何错误:

if(_db == null) {
    _db = new DbHelper(this);
}
try {
    _db.SaveExtraData();
    _db.CloseDb();
}
catch (Exception e) {

}

但我无法在设备上的文件系统中的任何位置找到数据库。我在Android版本为4.4.3的摩托罗拉TC55上进行了测试

任何帮助或建议将不胜感激。提前谢谢!

2 个答案:

答案 0 :(得分:1)

默认情况下,它位于/data/data/<packageNameOfYourApp>/databases/文件夹中,但除非您的设备已植根,否则无法访问此文件夹。

openDatabase()的{​​{1}}方法可让您为数据库提供路径。

例如:

SQLiteDatabase

查看documentation

或者,使用SQLiteDatabase.openDatabase("/pathToYourDb/yourDBFile.db", null, SQLiteDatabase.CREATE_IF_NECESSARY); 你可以这样做:

SQLiteOpenHelper

答案 1 :(得分:0)

有几种不同的方法可以访问您的数据库文件。您可以使用以下adb命令将数据库复制到本地计算机:

adb exec-out run-as APP_PACKAGE cat databases/DB_NAME.db > filename

另一种方法是实现备份和还原功能。运行备份时,将数据库复制到公共位置。还原时,将其复制回应用位置。这是一个示例恢复功能:

public void restoreDatabase() {
    try {
        File sd = Environment.getExternalStorageDirectory();
        File data = Environment.getDataDirectory();

        if (sd.canWrite()) {
            String backupDBPath = "//data//YOUR_PACKAGE//databases//DB_NAME.db";
            String currentDBPath = "sdcard/BACKUP_NAME.db";
            File currentDB = new File(currentDBPath);
            File backupDB = new File(data, 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) {
    }
}

使用上面代码的反向来备份。