lundi 10 août 2015

is it important to check if the dbname exiss or not

i am checking if the sqlite DB name exists or not before making connection as shown below in the code, but it seems that check is useless as long as whether the dbName exists or not, i have to run the below code:

Class.forName("org.sqlite.JDBC");
this.dbURL = "jdbc:sqlite:"+SysConsts.SQLITE_DATABASE_FOLDER+File.separator+this.dbName;

is it important to check if the dbname exiss or not? how to do it correctly?

code:

public void newSQLiteConn(String dbName) throws ClassNotFoundException, SQLException {
    if (dbName != null) {
        if (!dbName.isEmpty()) {
            if (!this.DBNameExist(dbName)) {

                this.dbName = dbName;
                Class.forName("org.sqlite.JDBC");
                this.dbURL = "jdbc:sqlite:"+SysConsts.SQLITE_DATABASE_FOLDER+File.separator+this.dbName;
                //this.ps = conn.prepareStatement("insert into "+TABLE_NAME+" ("+NODE_COL+", "+LAT_COL+", "+LNG_COL+", "+XML_PATH_COL+") values (?, ?, ?, ?)");

            }

            this.conn = DriverManager.getConnection(this.dbURL);
            this.stmt = conn.createStatement();

        } else {
            Log.e(TAG, "newSQLiteConn", "dbName is empty");
        }
    } else {
        Log.e(TAG, "newSQLiteConn", "dbName is null");
    }
}

private boolean DBNameExist(String dbName) {
    // TODO Auto-generated method stub
    File f = new File(SysConsts.SQLITE_DATABASE_FOLDER+File.separator+dbName);

    if (f.exists()) {
        Log.w(TAG, "newSQLiteConn", "dbName: "+dbName+" exists in "+SysConsts.SQLITE_DATABASE_FOLDER);
        return true;
    } else {
        return false;
    }

}

Aucun commentaire:

Enregistrer un commentaire