0

I have created a helper class to connect with my sqlite database, but I don't know how to call it to actually create database in /data folder in android.

This is my OpenHelper class:

public class DBHelper extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 2;
private static final String DATABASE_NAME = "database.db";
private static final String TAG = DBHelper.class.getSimpleName().toString();

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

@Override
public void onCreate(SQLiteDatabase db) {

    db.execSQL(categoryRepo.createTable());
    db.execSQL(word_polRepo.createTable());
    db.execSQL(word_engRepo.createTable());
    db.execSQL(wordRepo.createTable());
    db.execSQL(sentenceRepo.createTable());

    Log.d(TAG,"Database creating...");
    Log.d(TAG, "Table" +category.TABLE + "ver."+DATABASE_VERSION + "created");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    db.execSQL("DROP TABLE IF EXISTS" + category.TABLE);


    onCreate(db);
}

I want this class to execute and create database when app starts.

I tried something like this in MainActivity.java but it's not good.

DBHelper db = new DBHelper(this);
db.onCreate() ???
Prashant Sable
  • 1,003
  • 7
  • 19
ktos1234
  • 197
  • 1
  • 6
  • 22
  • http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/ see this... – Prashant Sable Nov 24 '16 at 08:38
  • you don't need to call onCreate() override method in other activity. It automatically call when database not exist. you have just initialize object and perform action. – Ramchandra Singh Nov 24 '16 at 08:39
  • @PrashantSable thanks for tutorial, if i have more than one table, should I create CRUD statements inside DataHelper class or should I create class for each table and put statements inside them? – ktos1234 Nov 24 '16 at 08:59
  • It depends on your coding style. You can create a different class for all the CRUD methods, it will make your code manageable. – Prashant Sable Nov 24 '16 at 09:03
  • @RamchandraSingh ok so i initialize object DBHelper db = new DBHelper(this) , but what action I can perform, I don't want to insert any row, I just want to get this tables to be created. Can I call getWritetableDatabase? – ktos1234 Nov 24 '16 at 09:19
  • If you want only table table then only initialization is enough for table create. – Ramchandra Singh Nov 24 '16 at 09:29

0 Answers0