-3

I am very new to android programming. I want to use SQLiteOpenHelper class to do some database operations. Here is my SqliteController class that extends SQLiteOpenHelper:

public class SqliteController extends SQLiteOpenHelper {
Context context;

public SqliteController(Context context) {
    super(context, "sectionsDB", null, 1);
    this.context = context;

}

@Override
public void onCreate(SQLiteDatabase db) {
    String query;
    query = "CREATE TABLE Sections ( chapterName TEXT, sectionName TEXT, body TEXT)";
    db.execSQL(query);
    Log.i("tablecreation", "table created");
}
}

(I have just mentioned the part that I have problem with) in my main activity I have created instance of above class in onCreate method of activity like this:

SqliteController controller = new SqliteController(this);

I want to populate database by reading from xml file once in onCreate method of SqliteController so after newing SqliteController in main activity I don't want to do any operation with database. If I add controller.getWritableDatabase(); it seems that I have some operations to do with it but all I do is in SqliteController class. Any suggestion?

Suzi
  • 89
  • 4
  • 16

1 Answers1

1

You can add something like this:

 SqliteController controller = new SqliteController(this);
 controller.getWritableDatabase();
Jose Angel Maneiro
  • 1,226
  • 9
  • 20
  • It works! but I don't know what does this line of code do? – Suzi Dec 25 '15 at 11:50
  • Does onCreate method execute without it? – Suzi Dec 25 '15 at 11:53
  • You need the second line because **controller.getWritableDatabase();** _Create and/or open a database that will be used for reading and writing._ [look this](http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html) – Jose Angel Maneiro Dec 25 '15 at 13:04
  • I just want to create and populate it once so I won't do anything with database that I get from controller.getWritableDatabase() – Suzi Dec 25 '15 at 14:11