Finding database path is impossible

后端 未结 2 675
有刺的猬
有刺的猬 2021-01-27 20:43

I am quite new to android development and right now I\'m trying to create a database through a class extending SQLiteOpenHelper. I am positively sure that the data is being sto

相关标签:
2条回答
  • 2021-01-27 21:05

    if you are trying to get database which is stored in applicaiton package from device thn you will not get direct access from device /data/data/app packagename/databases/ ..you can access and modify using helperclass bt direct export nt poss AFAIK ... bt from emulator u cn gt

    enter image description here

    If you to export database from in sd card from application package

    try this

    private void copyFromDataPackgeToSdCard() throws IOException {
        try {
            File sdCard = Environment.getExternalStorageDirectory();
            File appDataDir = Environment.getDataDirectory();
            if (sdCard.canWrite()) {
                String currentDBPath = "//data//" + getPackageName()
                        + "//databases//"
                        + DatabaseImplementation.DATABASE_NAME;
                String backupDBPath = DatabaseImplementation.DATABASE_NAME;
                File currentDatabase = new File(appDataDir, currentDBPath);
                File backupDatabase = new File(sdCard, backupDBPath);
    
                if (currentDatabase.exists()) {
                    FileChannel src = new FileInputStream(currentDatabase)
                            .getChannel();
                    FileChannel dst = new FileOutputStream(backupDatabase)
                            .getChannel();
                    dst.transferFrom(src, 0, src.size());
                    src.close();
                    dst.close();
                }
            }
        } catch (Exception e) {
            Log.e("copyFromDataPackgeToSdCard", e.getMessage());
        }
    }
    
    0 讨论(0)
  • 2021-01-27 21:15

    How about setting the dbname as full path:

    String DATABASE_NAME = "/data/data/"+packageName+"db.db";
    

    or put your db in SD card:

    String DATABASE_NAME= Environment.getExternalStorageDirectory()+packageName+"db.db";
    

    pass the dbPath in your code:

    public DatabaseImplementation(Context context) 
    {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    
    0 讨论(0)
提交回复
热议问题