0

EDIT: Removed Previous Code For Clarity

On suggestion, I added log messages to see where I am getting stopped up.

It appears that it is properly throwing the call to checkDatabase(), but not going back to copy the database. This is my checkDatabase() method:

File dbFile = new File(DB_PATH + DB_NAME);
        if(dbFile.exists()){
            Log.d("True","Returned true");
            return true;

        }else{
            dbFile.getParentFile().mkdirs();
            Log.d("CreatedPath","Made the right directory");
            return false;
        }

In the debug logs I can see my 'Created Path' message. So it's getting that far.

It then returns false, which should start this:

public void createDataBase() throws IOException{

        boolean dbExist = checkDatabase();
        if (!dbExist){

            this.getReadableDatabase();

            // Calling this method an empty database will be
            // created into the default system path of 
            // the app so we can overwrite with FirstDB.


            try{

                copyDatabase();

            }catch (IOException e) {
                throw new Error("Error copying database");

            }
        }
    }

However, I am not seeing those log messages at all. It's like it returns false and just keeps on moving.

Do I need to call createDataBase() again?

Phoenix
  • 1,881
  • 5
  • 20
  • 28
  • Please consider using the tested, debugged, and supported `SQLiteAssetHelper` instead of this code: https://github.com/jgilfelt/android-sqlite-asset-helper – CommonsWare Jul 10 '13 at 21:19
  • This did not work. Even a little bit. – Phoenix Jul 10 '13 at 22:29
  • It works for lots of other people, contrasted with the code base you are working from, which lots of people have problems with, as evidenced by all the StackOverflow questions about it. – CommonsWare Jul 10 '13 at 23:02

2 Answers2

2

I'd try this:

First, change that path string

private static String DB_PATH = "/data/data/com.example.mydbapp/databases/";

Next, do the same check for the file, but make the directory if it doesn't exist. You have to anyway; that's where the DB's will be saved by default by Android's SQLite implementation.

So try this:

private boolean checkDatabase(){
    File dbFile = new File(DB_PATH + DB_NAME);
    if(dbFile.exists()){
       return true;
    }
    else{
       //This'll create the directories you wanna write to, so you
       //can put the DB in the right spot.
       dbFile.getParentFile().mkdirs();
       return false;
    }
}

Anyway, that's just from a bit of googling, and what I know off the top of my head. Links at the bottom.

Also, I'm a little confused:

    //YOU NAME THIS myInput:
    InputStream myInput = firstDBContext.getAssets().open(DB_NAME);
    String outFileName = DB_PATH + DB_NAME;
    OutputStream bnOutput = new FileOutputStream(outFileName);
    byte[] buffer = new byte[1024];
    int length; 
    //YOU READ FROM bnInput:
    while ((length = bnInput.read(buffer)) > 0){
        bnOutput.write(buffer, 0, length);
    }

Maybe there's something I'm missing here, but aren't you reading from two different InputStreams? I also don't understand why you call this function in createDatabase():

    this.getReadableDatabase();

That function will call onCreate, which calls createDatabase, which will call onCreate again if checkDatabase() returns false, from what I can tell. Links to what I googled:

Java's createNewFile() - will it also create directories?

https://stackoverflow.com/a/9865386/2015759

http://docs.oracle.com/javase/7/docs/api/java/io/File.html#mkdirs%28%29

Create intermediate folders if one doesn't exist

Good luck.

Community
  • 1
  • 1
Shaquil Hansford
  • 383
  • 1
  • 2
  • 9
  • Thanks for the explanation. Your confusion is correct, that was a typo from a previous version of the attempt. I tend to name all my variables vaguely, and was trying to update here to make it make sense to the reader. I made the changes you suggested, still with the same outcome. Cannot open database. Something is stopping up before the creation, as I checked File Explorer and the directory does not exist. If that's the case it is not even hitting the `createDataBase()` method, as the first call in that is the `checkDatabase();` which would create the directory and return false. Thoughts? – Phoenix Jul 10 '13 at 21:44
  • Add calls to logcat where you think it's crashing. Put: Log.d("MESSAGE_TAG_HERE", "MESSAGE_HERE"); For example, in checkDatabase, in the condition if(db.exists()) put that Log.d code, with a message of "db exists!" Etc, etc. Also, in the logcat you've posted, it says there's an error at line 100 of DBSQLiteHelper, your file. It's in the openDatabase function. Maybe you'll find something there, I dunno. – Shaquil Hansford Jul 10 '13 at 22:15
  • Good idea. I'm about to go nuts, I have been trying to make this work for 3 days, and have spent several hours today trying to find solutions. – Phoenix Jul 10 '13 at 22:29
  • I'm thinking now, how do I instantiate my call to the DBSQLiteHelper? My app opens properly on my first Activity, it's when I go into my second activity (which attempts to use the database) that it fails. It's almost as though the db creation never gets called. – Phoenix Jul 10 '13 at 22:48
  • 1
    Got it working. It was the way I was calling the DBSQLiteHelper methods. I was starting in the middle, instead of at the top. Essentially I was calling `checkDataBase()` before the DB was created. It returned false to the method I was using to call it, which was all well and good, except I was never actually creating the database after the false return. Accepting this answer for the idea to add logcat calls. I didn't even know you could do that, but it totally cleaned up my debugging. Word. – Phoenix Jul 10 '13 at 23:40
  • The better debugging solution would be to learn how to make breakpoints work in eclipse with Android, but I never figured it out. Instead, as you see, logcat can be your best friend. Glad you figured it out. – Shaquil Hansford Jul 11 '13 at 00:31
1

Uninstall the app from your device and try again but add /databases/ to your DB_PATH.

private static String DB_PATH = "/data/data/com.example.mydbapp/databases/";

As fas as I know it should be always there and I do not see it on your createDatabase method.

Later try to acces via sqlite3 to check if it is correctly created.

According to docs

To use sqlite3, enter a remote shell on the emulator instance, as described above, then invoke the tool using the sqlite3 command. Optionally, when invoking sqlite3 you can specify the full path to the database you want to explore. Emulator/device instances store SQLite3 databases in the folder /data/data/< package_name >/databases/.

Here's an example:

adb -s emulator-5554 shell
# sqlite3 /data/data/com.example.mydbapp/databases/FirstDB.db`
AlexBcn
  • 2,450
  • 2
  • 17
  • 28