Search code examples
androidandroid-sqlite

Database connection is opened by creating object or using getwritabledatabase


My application has 7 activities and all are highly dependent on database and each activity when accessed will query database a lot.

For this issue I have created a singleton database object for the database class and for every operation I am calling below statement.

Database.getInstance(c1).getWritableDatabase()

for inserts, selects and deletes I am using like below

-- insert Database.getInstance(c1).getWritableDatabase().insert
-- select Database.getInstance(c1).getWritableDatabase().rawquery
-- delete Database.getInstance(c1).getWritableDatabase().rawquery

I am using this way because by this only one object is used in whole application and multiple objects won't be created.

But when searching the web, I learned that getWritableDatabase() will open a connection to database. Now I am confused whether object to database class will open a connection or calling getwritabledatabase will open a connection.

I am worrying because I feel I am creating too many connections unknowingly.

Edit

I have tried to create a writable database object as below in every activity and use that object in that actovity but it returned me database locked exception hence I was forced to use the Database.getInstance(c1).getWritableDatabase() for every db operation

I have created as below

Sqldatabase sd=Database.getInstance(c1).getWritableDatabase()

I am wanting to discover how the database should be opened and where it should be closed to make a application stable and avoid memory leaks. I am am a beginner to Android, and keen to find out if my design is a valid one.

Edit

Database file

public class Database extends SQLiteOpenHelper{

        private static  String dbname="Director";
        private static int dbversion=1;
        SQLiteDatabase db;
        private Context m1Context;
        private static Database minstance;
        public Database(Context context) {

            super(context, dbname, null, dbversion);
            // TODO Auto-generated constructor stub

        }

        public synchronized static Database getInstance(Context m1Context){

            if (minstance==null){

            minstance=new Database(m1Context);
            }
            return minstance;


        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub

            STable st=new StockTable(m1Context);
            BTable bt=new BrokerageTable(m1Context);
            SList sl=new StockList(m1Context);

            db.execSQL(st.stocktable);
            db.execSQL(bt.Brokerage);
            db.execSQL(sl.Create());
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub

        }


    }

class file:

public class SList {

    Context c1;
    Cursor getid;
    StockList(Context mContext){

        c1=mContext;
    }

    **//SQLiteDatabase sd=Database.getInstance(c1).getWritableDatabase();**
    String Ctable;
    //String ,selectIDgetstocks,deletestock;
    public String tablename="Stocklist";
    public String Column1="_id";
    public String Column2="Sname";
    ContentValues cv=new ContentValues();
    String getstocks="Select " + Column1 + " as _id, " + Column2 + " From "+ tablename;
    String selectID="Select Max("+  Column1 + ") from " + tablename;
    public String Create(){

        Ctable="Create Table " + tablename + " (" + Column1 + " INTEGER PRIMARY KEY , " + Column2 + " Text" + ")";

        return Ctable;
    }

    public void insert(int stockid,String name){

        cv.put(Column1, stockid);
        cv.put(Column2, name);
    ++Database.getInstance(c1).getWritableDatabase().insert(tablename,null,cv);++

    }


}

Solution

  • I believe class Database is wrapper for SQLiteOpenHelper, right? Since you have a singleton, each time you call getWritableDatabase() it's utilizing existing connection. The new connection is being called only at the first call. You can open new connection at onResume or onCreate and close it at onDestroy or onPause, for example.

    From the docs on getWritableDatabase():

    Once opened successfully, the database is cached, so you can call this method every time you need to write to the database