-1

I know that SQLiteDatabase doesn't have any public constructor and I cannot create its object directly.

But then how is SQLiteOpenHelper able to return SQLiteDatabase reference using getReadableDatabase() or getWritableDatabase()?

Barun
  • 4,245
  • 4
  • 23
  • 29
  • See http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html I assume the `createDatabase` and `openDatabase` functions call the private (or protected, not sure) constructor of `SQLiteDatabase`, since the static functions are declared in `SQLiteDatabase`. `SQLiteOpenHelper` internally calls those. – Reed Jan 05 '15 at 14:33
  • @Jakar Can you show me some demo Java code to understand you answer? – Barun Jan 05 '15 at 14:55
  • the code for SQLiteDatabase and SQLiteOpenHelper is open source, you can check out how it works. – njzk2 Jan 05 '15 at 15:17
  • see http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.1_r2/android/database/sqlite/SQLiteDatabase.java#SQLiteDatabase.openDatabase%28java.lang.String%2Candroid.database.sqlite.SQLiteDatabase.CursorFactory%2Cint%29 in particular – njzk2 Jan 05 '15 at 15:21
  • @Barun, If you haven't accepted an answer in a few hours, I'll post some example code to explain how it works. – Reed Jan 05 '15 at 16:00

2 Answers2

1

Just because the constructor isn't public/visible doesn't mean it doesn't exist!

SQLiteOpenHelper uses SQLiteDatabase's static constructor openDatabase to create a new database instance. It is public and has not been hidden so, although it is probably not a good idea, you could call it, directly, yourself.

According to the doc you would use it like this:

   SQLiteDatabase db = SQLiteDatabase.openDatabase(path, null); 

The first argument is a String giving the full path name for the new db. The second is the cursor factory. Passing null for the second arg will get you the default factory.

Using the SQLiteOpenHelper guarantees that the db is correctly created and correctly initialized. It also properly caches db instances, so that they aren't recreated every time you need them. It is probably better to use it.

G. Blake Meike
  • 6,615
  • 3
  • 24
  • 40
  • although `SQLiteDatabase.openDatabase` is not actually a constructor, I don't know how we should call it. I am not sure `static constructor` is the best term. – njzk2 Jan 05 '15 at 15:20
  • @njzk2 If it works for Joshua Bloch, it works for me. – G. Blake Meike Jan 05 '15 at 16:17
  • @G.BlakeMeike After looking at the source files at `android-sdk/sources/android-19/android/database/sqlite`, I believe your answer is the correct one. – Barun Jan 06 '15 at 13:06
  • Thank you @Barun! Looking at the source code is how I arrived at it! – G. Blake Meike Jan 06 '15 at 23:11
1

Try this:

Class Cdb.java:

public class Cdb extends SQLiteOpenHelper {

    private static final String DB_NAME = "db_name.db";
    public static final int DB_VERSION = 1;

    Cdb(Context context) {
        super(context.getApplicationContext(), DB_NAME, null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
        // Upgrade here
    }

    @Override
    public void onOpen(SQLiteDatabase db) {
        super.onOpen(db);

}

and then you may access the database as follows:

Cdb openHelper = new Cdb(this);
SQLiteDatabase db = openHelper.getReadableDatabase();
sec_aw
  • 1,594
  • 1
  • 15
  • 26
  • Your code is already shown in Android documentation and I understand it but what I wanted to know is any Java-related reason for getting reference that way. – Barun Jan 05 '15 at 15:38
  • @Barun the reason to it this way is so all your db create/ upgrade code is in one file. Take a look at this http://stackoverflow.com/questions/22440195/how-to-invoke-onupgrade-in-android-for-work-with-sqlite-database/22444918#22444918 and this http://stackoverflow.com/questions/19793004/android-sqlite-database-why-drop-table-and-recreate-on-upgrade/19836980#19836980 – danny117 Jan 05 '15 at 19:08