如何查看我的 Android 申请数据库? [Android]

How can I view my Android application Database ? [Android]

如何在 phone 上获取应用程序 运行 的数据库。 我使用 Android Studio,我的数据库在 assets>MyDataBase.db 有没有办法更新这个文件?

我有自己的 sqlite 数据库,我插入数据然后我想用新数据取回文件。

我不使用模拟器,我的数据库是由 SQLite 浏览器创建的。

[编辑]

人们说这是 this 问题的重复。 但是我想在代码行而不是命令中做同样的事情。

是的,您可以通过多种方式进行编辑。其中之一是使用 this 等库。看看它并在您的应用程序中试用它。

如果您想在运行时编辑应用程序资产文件夹中的文件 - 这是不可能的。您应该将此数据库复制到 internal/external 存储并在之后进行编辑。

您不能将数据写入 asset/Raw 文件夹,因为它是打包的 (.apk),资产文件夹在运行时是 read-only。

您需要选择不同的存储位置,这里有一种方法可以将您的数据库备份到您的 sdcard(外部存储)dbName=MyDataBase.db 在您的情况下:

public static void backupDBonSDcard(Context context, String dbName){
    String DB_PATH = context.getDatabasePath(dbName).getPath();
    Log.d("DB_PATH:" + DB_PATH);
    if(checkDataBase(DB_PATH)){
        InputStream myInput;
        try {
            Log.e("[backupDBonSDcard] saving file to SDCARD");
            myInput = new FileInputStream(DB_PATH);

            // Path to the just created empty db
            String outFileName = Environment.getExternalStorageDirectory().getAbsolutePath() + java.io.File.separator + dbName;

            //Open the empty db as the output stream
            OutputStream myOutput;
            try {
                myOutput = new FileOutputStream(outFileName);
                //transfer bytes from the inputfile to the outputfile
                byte[] buffer = new byte[1024];
                int length;
                while ((length = myInput.read(buffer))>0){
                    myOutput.write(buffer, 0, length);
                }
            } catch (FileNotFoundException | IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                //Close the streams
                if(myOutput!=null){
                    myOutput.flush();
                    myOutput.close();
                }

                if(myInput!=null)
                    myInput.close();
            }
        } catch (FileNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

    }else{
        Log.d("DB "+dbName+" not found");
    }
}

public static boolean checkDataBase(String fileName) {
    java.io.File dbFile = new java.io.File(fileName);
    return dbFile.exists();
}

不要忘记在清单中添加以下权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />