0

I have a problem with sqlite open helper i receive this error:

sqlite3_open_v2("/data/data/lam.ztl.lamztlbologna/databases/ztlBolo.db", &handle, 2, NULL) failed Failed to open the database. closing it. android.database.sqlite.SQLiteCantOpenDatabaseException: unable to open database file I don't understand why receive this error.

My Open helper class is:

enter code here



public class DBHelper extends SQLiteOpenHelper {
    private final static String DATABASE_NAME = "ztlBolo.db";
    static SQLiteDatabase db;
    //private  static String DB_PATH = "/data/data/lam.ztl.lamztlbologna/databases/";
    static Context context;
    private  static String DB_PATH = "/data/data/"+context.getPackageName()+"/databases/";



    private final static String TABLE_NAME = "manageZtlStreet";
    private final static String SQL_PATH = "databaseZtlBolo.sql";
    MapsActivity ma = new MapsActivity();
    Double LATITUDE= 0.0;
    Double LONGITUDE = 0.0;
    private final Context myContext;
    private final String CREATE_TABLE_ZTL_STREET = "CREATE TABLE manageZtlstreet ("
            + " via text not null,"
            +"latitude DOUBLE," 
            + "longitude DOUBLE);";

    public DBHelper(Context context,int version) {

        super(context, DBHelper.DATABASE_NAME,null,1);
        this.myContext = context;
    }

    @Override
    public void onCreate(SQLiteDatabase database) {
        //  database.openOrCreateDatabase(database.getPath(), null);
        database=SQLiteDatabase.openDatabase(database.getPath(), null, SQLiteDatabase.NO_LOCALIZED_COLLATORS|SQLiteDatabase.OPEN_READWRITE|SQLiteDatabase.CREATE_IF_NECESSARY);
        database.execSQL(this.CREATE_TABLE_ZTL_STREET);

        //DB_PATH = db.getPath();
        try {

            copyDataBase(database);
            Log.e("copy","copy");

        } catch (IOException e) {

            throw new Error("Error copying database");

        }
        database.close();
    }






    /**
     * Copies your database from your local assets-folder to the just created empty database in the
     * system folder, from where it can be accessed and handled.
     * This is done by transfering bytestream.
     * */
    private void copyDataBase(SQLiteDatabase db) throws IOException{


        //Open your local db as the input stream
        InputStream myInput = myContext.getAssets().open(SQL_PATH);
        /*leggo il file*/
        BufferedReader buffread =  new BufferedReader(new InputStreamReader(myInput));
        String line = null;

        while((line = buffread.readLine()) != null) {

            //Log.e("line",""+line);
            //db.execSQL(line);!
            if(!line.equals(new String(""))){
                db.execSQL(line);
            }
        }

        myInput.close();
    }

        public void openDataBase() throws SQLException{
        SQLiteDatabase myDataBase = null;
        String myPath = DATABASE_NAME;
        myDataBase = SQLiteDatabase.openDatabase( DB_PATH+DATABASE_NAME, null, SQLiteDatabase.OPEN_READWRITE);
        Log.e("path",""+myDataBase);

    }

    @Override
    public synchronized void close() {

        /*  if(myDataBase != null)
            myDataBase.close();
         */
        super.close();

    }


    @Override
    public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
        database.execSQL("DROP TABLE IF EXISTS manageZtlStreet;");
        //database.close();
        this.onCreate(database);
    }


    public Cursor getZtlStreet(){
        String query = "select latitude ,longitude from manageZtlstreet";

        SQLiteDatabase rdb = getWritableDatabase();
        /** Cursor cursor=rdb.rawQuery(query,null);
        rdb.close();
        return cursor;*/return null;
    }
4
  • I would look at this answer: stackoverflow.com/questions/6356170/… Commented Apr 22, 2013 at 14:59
  • can you write exceptions Commented Apr 22, 2013 at 15:00
  • i receive exception unable to open database file , i don't have a number of exception Commented Apr 22, 2013 at 17:39
  • Solved: i see two database.close() and i erase on db helper Commented Apr 26, 2013 at 14:55

1 Answer 1

1

You shouldn't use file ztlBolo.db directly.

Bad idea: private static String DB_PATH = "/data/data/"+context.getPackageName()+"/databases/";

My implementation:

public class UtilDB {

private static final String DATABASE_NAME = "cool_db.db";
private static final int DATABASE_VERSION = 1;

private final String SQL_CREATE_MY_COOL_TABLE = "CREATE  TABLE `cool_table` ...";


private SQLiteDatabase mDB = null;
private MyDBHelper mDBHelper = null;
private static UtilDB mInstance = null;

//Using pattern singleton
public static UtilDB getInstance(Context context) {
    if (mInstance == null) {
        mInstance = new UtilDB(context);
    }

    mInstance.open();

    return mInstance;
}

private UtilDB(Context context) {       
    mDBHelper = new MyDBHelper (context, DATABASE_NAME, null, DATABASE_VERSION);
}

private void open() throws SQLException {
    //If connection to db is not open then will open connection
    if ((mDB == null) || (!mDB.isOpen())) {
        mDB = mDBHelper.getWritableDatabase();
    }
}

public void close() {
    mDB.close();
}


private class MyDBHelper extends SQLiteOpenHelper {

    public MyDBHelper (Context context, String name, CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase _db) {
        _db.execSQL(SQL_CREATE_MY_COOL_TABLE );
    }

    @Override
    public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion) {
        _db.execSQL("DROP TABLE IF EXISTS \"cool_table\"");

        onCreate(_db);
    }
}

}

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.