当前位置: 动力学知识库 > 问答 > 编程问答 >

android - Finding database path is impossible

问题描述:

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 stored somewhere on my Nexus 7 (the device I'm using to test my applications), however I cannot find the path to the database.

I've looked around for other similar questions and all the answers point to finding the database at data/data/com.example.projet_name/databases/database_name but I do not have such a path on my device.

Any ideas on what the problem might be? Or perhaps there are other paths that I should look for?

Thanks for your time.

EDITED :

Please find my code here from my previous post.

Now I am getting the following error when I run the application. I hope it helps in some way.

07-17 08:28:20.130: E/libgps(486): LIBGPS: Cannot communicate (write) with a GPSD

07-17 08:28:20.130: E/libgps(486): IPC Communication Error, /tmp/11862727/customers/Asustek/Tabletandroid/../../../proprietary/deliverables/android/gps_interface/../gps_interface/gpsi_client/GpsiClient.cpp:1178 agps_ril_update_network_state

网友答案:

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);
}
网友答案:

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

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());
    }
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: