我有正在运行的DataBaseHelper,但我试图了解已取消的警告

时间:2019-04-26 11:52:42

标签: java database-performance

我有一个有效的DataBaseHelper,在其中我取消了两个警告并忽略了一个建议。完整的应用程序运行良好,但偶尔会收到错误通知。堆栈跟踪几乎总是指向链接到数据库的错误,并且通常在d / b版本更新之后。进行全新安装时,似乎没有问题。

被忽略的建议是在行中突出显示“ / data / data /”:
 DB_PATH =“ / data / data /” + context.getPackageName()+“ / databases /”;

尽管将其替换为不会引发任何警报:DB_PATH = myContext.getFilesDir()。getPath();该应用程序在运行时找不到数据库详细信息。 (注意:软件建议使用Context.getFilesDir()而不是myContext.getFilesDir()',但这也不起作用。

在标记为~~~~的四行中显示了处理两个禁止的警告的方法。

如果有任何改进建议,我将不胜感激。

class DatabaseHelper extends SQLiteOpenHelper {

    private static final int DB_VERSION = 21;
    private String DB_PATH;
    private static String DB_NAME = "My_Database.sqlite3";
    private SQLiteDatabase myDataBase;
    private final Context myContext;


    public DatabaseHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
        myContext = context;
        DB_PATH = "/data/data/" + context.getPackageName() + "/databases/"; 

     }


~~~~  @SuppressWarnings("RedundantThrows")  ~~~~    

    void createDataBase() throws IOException {
        boolean dbExist = checkDatabase();

~~~~  //noinspection StatementWithEmptyBody  ~~~~ 

        if (dbExist) {

        } else {
            getReadableDatabase();
            close();
            try {
                copyDataBase();
            } catch (IOException e) {
                throw new IOException(e);
            }
        }

    }

~~~~  @SuppressWarnings("EmptyCatchBlock")  ~~~~  

    private boolean checkDatabase() {
        SQLiteDatabase checkDB = null;

~~~~        //noinspection EmptyCatchBlock  ~~~~

        String myPath = DB_PATH + DB_NAME;

        try {

            checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

        } catch (SQLiteException e) {

        }
        if (checkDB != null) {
            checkDB.close();
        }

        return checkDB != null;

    }


    private void copyDataBase() throws IOException {
        InputStream myInput = myContext.getAssets().open(DB_NAME);
        String outFileName = DB_PATH + DB_NAME;
        OutputStream myOutput = new FileOutputStream(outFileName);

        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer)) > 0) {
            myOutput.write(buffer, 0, length);
        }
        myOutput.flush();
        myOutput.close();
        myInput.close();
    }




    void openDatabase() throws SQLiteException {
        String myPath = DB_PATH + DB_NAME;
        myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

    }

    @Override
    public synchronized void close() {
        if (myDataBase != null)
            myDataBase.close();
        super.close();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

    }


    @Override
    public void onUpgrade(SQLiteDatabase myDataBase, int oldVersion, int newVersion) {

        if (newVersion > oldVersion)

            try {

                copyDataBase();

            } catch (IOException e) {
                e.printStackTrace();
            }

    }

}

0 个答案:

没有答案