1

So I'm not sure what happened and where My database was working perfectly I was running queries and everything and then one build later boom! Now it won't open the database. I've tried deleting the database from the assets folder and clearing everything out and putting it back in several different attempts at different methods and still no luck. I'm at my wit's end here.

07-27 12:43:57.536 29216-29216/com.apklegion.cltoollog E/SQLiteLog: (14) 
cannot open file at line 35671 of [8201f4e1c5]
(14) os_unix.c:35671: (2) 
open(/data/user/0/com.apklegion.cltoollog/databases/cltooldb.db) - 
07-27 12:43:57.544 29216-29216/com.apklegion.cltoollog E/SQLiteDatabase: 
 Failed to open database 
'/data/user/0/com.apklegion.cltoollog/databases/cltooldb.db'.
android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 
14): Could not open database
    at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
    at 
android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
    at 
android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
    at 

  android.database.sqlite.SQLiteConnectionPool
.openConnectionLocked(SQLiteConnectionPool.java:463)
    at 
 android.database.sqlite.SQLiteConnectionPool.open
 (SQLiteConnectionPool.java:185)
    at android.database.sqlite.SQLiteConnectionPool.open
 (SQLiteConnectionPool.java:177)
    at android.database.sqlite.SQLiteDatabase.openInner
(SQLiteDatabase.java:808)
    at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:793)
    at android.database.sqlite.SQLiteDatabase.openDatabase
(SQLiteDatabase.java:696)
    at android.database.sqlite.SQLiteDatabase.openDatabase
(SQLiteDatabase.java:671)
    at com.apklegion.cltoollog.DatabaseHelper.DBExists
(DatabaseHelper.java:82)
    at com.apklegion.cltoollog.DatabaseHelper.createDataBase
(DatabaseHelper.java:58)
    at com.apklegion.cltoollog.MainActivity.onCreate(MainActivity.java:45)
    at android.app.Activity.performCreate(Activity.java:6984)
    at android.app.Instrumentation.callActivityOnCreate
(Instrumentation.java:1235)
    at android.app.ActivityThread.performLaunchActivity
(ActivityThread.java:2783)
    at android.app.ActivityThread.handleLaunchActivity
(ActivityThread.java:2909)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1606)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6592)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run
(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:769)
07-27 12:43:57.544 29216-29216/com.apklegion.cltoollog E/SQLHELPER: database 
not found
07-27 12:43:57.621 29216-29216/com.apklegion.cltoollog D/AndroidRuntime: 
Shutting down VM


--------- beginning of crash
07-27 12:43:57.625 29216-29216/com.apklegion.cltoollog E/AndroidRuntime: 
FATAL EXCEPTION: main
Process: com.apklegion.cltoollog, PID: 29216
java.lang.Error: Problem copying database from resource file.
    at 

  com.apklegion.cltoollog.DatabaseHelper.copyDBFromResource
 (DatabaseHelper.java:124)
    at 
com.apklegion.cltoollog.DatabaseHelper.createDataBase(DatabaseHelper.java:64)
    at com.apklegion.cltoollog.MainActivity.onCreate(MainActivity.java:45)
    at android.app.Activity.performCreate(Activity.java:6984)
    at 

 android.app.Instrumentation.callActivityOnCreate
(Instrumentation.java:1235)
    at android.app.ActivityThread.performLaunchActivity
(ActivityThread.java:2783)
    at android.app.ActivityThread.handleLaunchActivity
(ActivityThread.java:2909)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1606)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6592)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run
(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:769)

Here is my Database helper class

public  class DatabaseHelper extends SQLiteOpenHelper {

public static String TAG = "DatabaseHelper";
public static final String DATABASE_NAME = "cltooldb.db";
public static final String TABLE_NAME ="toolstable";
public static  String DATABASE_PATH = "";
public static final int DATABASE_VERSION =11;
private SQLiteDatabase mDataBase;

private final Context myContext;

public DatabaseHelper(Context context){

    super(context,DATABASE_NAME,null,DATABASE_VERSION);
    this.myContext = context;
    if(android.os.Build.VERSION.SDK_INT >= 17){
        DATABASE_PATH = context.getApplicationInfo().dataDir + "/databases/";
    }
    else
    {
        DATABASE_PATH = "/data/data/" + context.getPackageName() + "/databases/";
    }



}

public void onCreate(SQLiteDatabase db){

}

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

}

public void createDataBase()throws  IOException{
    boolean dbExist = DBExists();

    if(!dbExist) {
        this.getReadableDatabase();
        this.close();
        //Copy the database from assests
        copyDBFromResource();
        Log.e(TAG, "createDatabase database created");

    }

}


private void createDB(){


}

private boolean DBExists(){
    SQLiteDatabase db = null;

    try{
        String databasePath = DATABASE_PATH + DATABASE_NAME;
        db = SQLiteDatabase.openDatabase(databasePath, null, SQLiteDatabase.OPEN_READWRITE);
        db.setLocale(Locale.getDefault());
        db.setLockingEnabled(true);
        db.setVersion(12);

    }catch(SQLiteException e){
        Log.e("SQLHELPER", "database not found");
    }
    if(db != null){

        db.close();
    }
    return  db != null ? true : false;

}
private boolean checkDataBase() {
    File dbFile = new File(DATABASE_PATH + DATABASE_NAME);
    Log.i("dbFile", dbFile + "   " + dbFile.exists());
    return dbFile.exists();
}

private void copyDBFromResource() {


     ;
    try {

        InputStream   inputStream = myContext.getAssets().open(DATABASE_NAME);
        String dbFilePath = DATABASE_PATH + DATABASE_NAME;
         OutputStream outStream = new FileOutputStream(dbFilePath);

        byte[] buffer = new byte[1024];
        int length;
        while ((length = inputStream.read(buffer)) > 0) {
            outStream.write(buffer, 0, length);
        }
        Log.i("Tools","Successfully added");
        outStream.flush();
        outStream.close();
        inputStream.close();

    } catch (IOException e) {
        throw new Error("Problem copying database from resource file.");
    }

}

public boolean openDataBase() throws SQLException {
    String mPath = DATABASE_PATH + DATABASE_NAME;
    //Log.v("mPath", mPath);
    mDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.CREATE_IF_NECESSARY);

    return mDataBase != null;
}


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


public Cursor getPartNumber(String desc){

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor c = db.rawQuery("select part_number, serial_number from 
"+TABLE_NAME+" where desc8ription like '"+desc+"'",null);
return c;

 }


}

and last the part that's in my main activity

    DatabaseHelper databaseHelper = new DatabaseHelper(this);
    try {
        databaseHelper.createDataBase();
    } catch (IOException e) {
        e.printStackTrace();
    }
apk legion
  • 21
  • 1
  • 10
  • Checking for its existence from a file path seems a bit unconventional. What are you trying to achieve? That the database in your resources gets copied in the first time it runs? If so, you can just put that in the DatabaseHelper `onCreate` method (https://stackoverflow.com/a/21881993/9473786) – Tyler V Jul 27 '18 at 17:17
  • Yeah that's what im trying to do. I believe that's the root of the problem here – apk legion Jul 27 '18 at 17:21
  • Ok, it might be easier to copy the *data* from the resources database to the current user database in `onCreate`, rather than trying to copy the file itself. The `onCreate` method is called automatically when the app's database is first created. – Tyler V Jul 27 '18 at 17:25
  • Ok so I should move the code from crateDatabase to the oncreate method I left blank? Or is there more to it than that – apk legion Jul 27 '18 at 18:12
  • Well I was thinking that instead of copying the database from assets, you would open it and copy the data from it to the new one in the `onCreate` method. I don't know how complex your data is, but if it's simple it would be easier to just initialize the database in `onCreate`. If for some reason you can't do that, you'll have to open the one in assets and copy the data over. – Tyler V Jul 27 '18 at 18:16
  • The database I'm using g has over 800 lines of data – apk legion Jul 27 '18 at 18:54
  • This worked before I'm not sure what changed – apk legion Jul 27 '18 at 18:55
  • Yeah, no idea on that one. Have you tried using `context.getDatabasePath(DATABASE_NAME);` to get the path? (https://developer.android.com/reference/android/content/Context#getDatabasePath(java.lang.String) Note that it states: "The returned path may change over time if the calling app is moved to an adopted storage device, so only relative paths should be persisted." – Tyler V Jul 27 '18 at 19:02
  • So the path it's showing in the error /data/user/0 isn't the one I should use? I'll check and see what that returns – apk legion Jul 27 '18 at 19:13
  • I'm not sure, but hard coding the different paths seems like a possible source of error. It's worth at least comparing to `getDatabasePath` to see if they're different... – Tyler V Jul 27 '18 at 19:15

0 Answers0