1

I have an SQLite database file, created using Sqliteman admin utility. I copied the database file that I created into my Eclipse project's "Assets" folder (should I have done export to a .sql file?).

So in the code I created my own class for opening and using databases from existing file paths. Here is the relevant code:

    private static String DB_FULL_PATH_EXISTING = "/data/data/com.jds.databasetest/databases/Book1"; 

Where com.jds.databasetest is my package name, and Book1 is the name of the database file in the assets folder. Now in some activity:

    SQSimple existingDB = new SQSimple(this, "Book1", DB_FULL_PATH_EXISTING);
    existingDB.open();

SQSimple is my custom DB class. Here is the relevant constructor code:

public SQSimple(Context c, String DBname, String existingDBpath) {
        this.mCtx = c; 
        this.DBname = DBname;
        this.DBpath = existingDBpath; 
        this.fromExisting = true; 
    }

public SQSimple open() throws android.database.SQLException {
        if (this.fromExisting == false) {
            dbHelper = new DatabaseHelper(this); 
            db = dbHelper.getWritableDatabase();
            return this; 
        } else { //opening from existing DB, i.e. this code gets run
            db = SQLiteDatabase.openDatabase(this.DBpath, null, SQLiteDatabase.OPEN_READWRITE);
            return this;
        }
    }

So just trying to open the database via existingDB.open() causes the app the crash. Logcat says the following:

 E/Database(13144): sqlite3_open_v2("/data/data/com.jds.databasetest/databases/Book1", &handle, 2, NULL) failed

Hopefully what I'm doing is pretty well-defined and I'm missing something rather obvious here.

Thank you very much for any help.

Fluffeh
  • 33,228
  • 16
  • 67
  • 80
JDS
  • 16,388
  • 47
  • 161
  • 224
  • Also, I would like to add: I'm not sure what the dbHelper that is so commonly used would be for in my case. I have the file path... Can I not just open the thing with SQLiteDatabase.openDatabase() ? – JDS Aug 05 '12 at 02:11
  • http://stackoverflow.com/questions/9109438/how-to-use-existing-database-with-android-app/9109728#9109728 – Yaqub Ahmad Aug 06 '12 at 05:03

1 Answers1

1

I'm not sure exactly but maybe you need a reference to Data?

File data = Environment.getDataDirectory();
NottG
  • 26
  • 1
  • I'm not sure how that would help. I've been following examples where they just use a filepath as a string to the database: http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/ – JDS Aug 05 '12 at 02:10
  • Yea, I just remember that when backing up a database from Data to SD you'll need that reference, sorry. Good luck. – NottG Aug 05 '12 at 02:12