androidBoomer
androidBoomer

Reputation: 3417

Unable to open sqlite database file

Can someone help me with the reason why errors keeps on appearing.

I have spinners and EditTexts in my activity. In that activity, it already shows the android.database.sqlite.SQLiteCantOpenDatabaseException: unable to open database file.

Here is what my Logcat says:

07-03 16:43:03.156: E/SqliteDatabaseCpp(13449): sqlite3_open_v2("/data/data/ph.com.unilab.ireport/databases/lamp_db.sqlite", &handle, 1, NULL) failed
07-03 16:43:03.164: E/SQLiteDatabase(13449): Failed to open the database. closing it.
07-03 16:43:03.164: E/SQLiteDatabase(13449): android.database.sqlite.SQLiteCantOpenDatabaseException: unable to open database file
07-03 16:43:03.164: E/SQLiteDatabase(13449):    at android.database.sqlite.SQLiteDatabase.dbopen(Native Method)
07-03 16:43:03.164: E/SQLiteDatabase(13449):    at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1123)
07-03 16:43:03.164: E/SQLiteDatabase(13449):    at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1074)
07-03 16:43:03.164: E/SQLiteDatabase(13449):    at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1050)
07-03 16:43:03.164: E/SQLiteDatabase(13449):    at ph.com.unilab.ireport.DatabaseHandler.checkDataBase(DatabaseHandler.java:81)
07-03 16:43:03.164: E/SQLiteDatabase(13449):    at ph.com.unilab.ireport.DatabaseHandler.createDB(DatabaseHandler.java:49)
07-03 16:43:03.164: E/SQLiteDatabase(13449):    at ph.com.unilab.ireport.S_9th_ISubmit.onCreate(S_9th_ISubmit.java:56)
07-03 16:43:03.164: E/SQLiteDatabase(13449):    at android.app.Activity.performCreate(Activity.java:4465)
07-03 16:43:03.164: E/SQLiteDatabase(13449):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
07-03 16:43:03.164: E/SQLiteDatabase(13449):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
07-03 16:43:03.164: E/SQLiteDatabase(13449):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
07-03 16:43:03.164: E/SQLiteDatabase(13449):    at android.app.ActivityThread.access$600(ActivityThread.java:128)
07-03 16:43:03.164: E/SQLiteDatabase(13449):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
07-03 16:43:03.164: E/SQLiteDatabase(13449):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-03 16:43:03.164: E/SQLiteDatabase(13449):    at android.os.Looper.loop(Looper.java:137)
07-03 16:43:03.164: E/SQLiteDatabase(13449):    at android.app.ActivityThread.main(ActivityThread.java:4514)

I can populate the data from the database to spinner, but however, wherever I press the Save button, the values from the widgets didn't saved to the database.

databaseHandler.java

public class DatabaseHandler extends SQLiteOpenHelper {
static final String TAG = "LAMP";
public SQLiteDatabase db;   
private final Context myContext;    
private static DatabaseHandler dbHandler;


public DatabaseHandler(Context context) {
    super(context, Constants.DB_NAME, null, Constants.DB_VERSION);
    this.myContext = context;
}

public static DatabaseHandler getInstance(Context context) {
        if(dbHandler == null) {
        dbHandler = new DatabaseHandler(context);
        }
    return dbHandler;
    }


    public void createDB() throws IOException{
    boolean dbExist = checkDataBase();
    if(dbExist) {
            //do nothing - database already exist
    }else{
         this.getReadableDatabase();
         try {
             copyDataBase();
         } catch (IOException e) {
             throw new Error("Error copying database");
         }
     }
}


    private boolean checkDataBase(){
    SQLiteDatabase checkDB = null;
    try{
            String myPath =Constants.DB_PATH + Constants.DB_NAME;
        checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
     }catch(SQLiteException e){  
            //database does't exist yet.     
    }

        if(checkDB != null){

            checkDB.close();

        }

        return checkDB != null ? true : false;
    }


    private void copyDataBase() throws IOException{

        //Open your local db as the input stream
        InputStream myInput = myContext.getAssets().open(Constants.DB_NAME);

        // Path to the just created empty db
        String outFileName = Constants.DB_PATH + Constants.DB_NAME;

        //Open the empty db as the output stream
        OutputStream 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);
        }
        //Close the streams
        myOutput.flush();
        myOutput.close();
        myInput.close();

    }


    public void openDataBase() throws SQLException{

        //Open the database
        String myPath = Constants.DB_PATH + Constants.DB_NAME;
        db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

    }

    @Override
    public synchronized void close() {

            if(db != null)
                db.close();

            super.close();

    }

    @Override
    public void onCreate(SQLiteDatabase db) {

    }

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

    }

Can you help resolve this error?

Upvotes: 2

Views: 6597

Answers (2)

Aditya
Aditya

Reputation: 5619

As dasdasd said, use

this.getWritableDatabase();
instead of
this.getReadableDatabase();

And, Create the database in onCreate. OnCreate is called when the database is created for the first time. This is where the creation of tables and the initial population of the tables should happen.

For example

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE IF NOT EXISTS " + tableName + "(" + id
            + " INTEGER PRIMARY KEY AUTOINCREMENT, " + startDate
            + " LONG , " + url + " TEXT, " + title + " TEXT, " + endDate
            + " LONG, " + sentToCloud + " INTEGER DEFAULT 0 );");

}

Upvotes: 3

dasdasd
dasdasd

Reputation: 2031

You have this line:

this.getReadableDatabase();

You can write with this method.

Change it to this:

this.getWritableDatabase();

Upvotes: 1

Related Questions