Search code examples
androidandroid-studioandroid-sqlite

Any way to get internal stored SQLite database using my code?


I am trying to access to my early stored .db file through my Java code:

    String path = "/data/data/" + this.getPackageName() + "/databases/" + DBHelper.DATABASE_NAME;
    SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(path, null);

And this is where the .db file stored: enter image description here

Raw path: /data/data/com.xxx.xxx/databases/database.db

But when compiling, Android Studio keeps warning me this: (DATABASE_NAME refers database.db)

E/SQLiteLog: (1) no such table: database.db

MainActivity.java

private void startMatching() {
    DBHelper helper = new DBHelper(this);
    SQLiteDatabase write = helper.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(DBHelper.COLUMN_NAME_ORIGINAL, original);
    
    write.insert(DBHelper.TABLE_NAME, null, values);
}

DBHelper.java

public static final String DATABASE_NAME = "database.db";
public static final Integer DATABASE_VERSION = 1;
public static final String TABLE_NAME = "mTable";

public DBHelper(Context context) {
    super(
            context,
            DATABASE_NAME,
            null,
            DATABASE_VERSION
    );
}

I have no idea why this error occurs. Anyone can help? :(


Solution

  • In android, when you use SqlLiteDatabase, you don't really specify the path. All of this is taken care of for you. Here is some good documentation to follow to get your database started: https://developer.android.com/training/data-storage/sqlite

    In short, your basic database class will look like:

    public class FeedReaderDbHelper extends SQLiteOpenHelper {
        // If you change the database schema, you must increment the database version.
        public static final int DATABASE_VERSION = 1;
        public static final String DATABASE_NAME = "FeedReader.db";
    
        public FeedReaderDbHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(SQL_CREATE_ENTRIES);
        }
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // This database is only a cache for online data, so its upgrade policy is
            // to simply to discard the data and start over
            db.execSQL(SQL_DELETE_ENTRIES);
            onCreate(db);
        }
        public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            onUpgrade(db, oldVersion, newVersion);
        }
    }
    

    Note how the DB name is JUST a name, NOT a path. you will also have a 'contract' class to define your table constants which you can then use when you create those tables, as well as read/write to and from those tables. Thoroughly read the linked tutorial and you should be good to go!

    However, if you want to use a pre-populated database, I suggest you look into SQLiteOpenHelper instead.