Search code examples
androiddatabasesqlitesqliteopenhelper

how to create database once only, then read and write multiple times from it, SQLite, OpenHelper, Android


I made two classes, the main class that extends Activity and the Database class that holds the database and it's methods. parts of the code from the database class are shown below.

the SQLiteOpenHelper class is a nested class inside the database class. I took it from an example I found on the internet. inside of this nested class is the method,

 db.execSQL(SCRIPT_CREATE_DATABASE);

how do I create a new database? If I instantiate the Database class from the Main class like this:

Database db = new Database(this);

does that instantiation automatically instantiate the nested SQLiteOpenHelper class too? so i don't have to explicitly do that.

however for this example of how to create a database, i am confused. if every time I instantiate a new instance calling the addNewRow() method like this:

  public void addNewRow(String label, int price){
  Database db = new Database(context);
     db.openToWrite();
     db.insertNewRow(checkBoxStatus, label, price);
     db.close();
 }

then a new database is created on the "new Database(context)" call, and next I add the info to enter into the columns. and finally call db.close(), however every time i call the addNewRow method shown above, it will instantiate a new database and that also instantiates SQLiteOpenhelper class so a new database is created. that means the last database has been overwritten, and my last row added has been lost, is this correct?

how do i use this Database class to create a Database only once and then read and write things from it with multiple calls like this?

 Database db = new Database(context);
  db.openToWrite(); or db.openToRead();
 // read or update or create new row in database
 db.close();

the database class:

  public class Database {

    public static final String MYDATABASE_NAME = "my_database";
    public static final String MYDATABASE_TABLE = "my_table";
    public static final int MYDATABASE_VERSION = 1;
    public static final String KEY_CHECKBOX_STATUS = "check_box_status";
    public static final String KEY_CHECKBOX_LABEL = "check_box_label";
    public static final String KEY_PRICE = "price"; 

  //create table MY_DATABASE (ID integer primary key, Content text not null);
    private static final String SCRIPT_CREATE_DATABASE =
     "CREATE TABLE " + MYDATABASE_TABLE + " (" + "ID INTEGER PRIMARY KEY AUTOINCREMENT, " +  
    "KEY_CHECKBOX_STATUS INTEGER, " + "KEY_CHECKBOX_LABEL TEXT, " + " KEY_PRICE INTEGER" + ");";

    SQLiteDatabase sqLiteDatabase;
    SQLiteHelper sqLiteHelper;
    Context context;

    public Database(Context c){
          context = c;
         }

      // after this all the rest of the methods for get and set of database values

code for the SQLiteOpenHelper class, nested inside of the Database Class:

 public class SQLiteHelper extends SQLiteOpenHelper {

          public SQLiteHelper(Context context, String name,
            CursorFactory factory, int version) {
           super(context, name, factory, version);
          }

          @Override
          public void onCreate(SQLiteDatabase db) {
           // TODO Auto-generated method stub
           db.execSQL(SCRIPT_CREATE_DATABASE);
          }

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

          }

         }

Solution

  • Yes, every time you instantiate a Database class a SQLiteHelper is instantiate. But the SQLiteHelper onCreate is only called if the database does not exist. You can test this by adding a new column to one of the table and then try to insert a row having value in this column then your app will crash. The error would be "no such column". You then need to clear your data or change the version of your database or uninstall your app to have your change table to be recreated.