I want to insert records into my sqlite database only when the database is created (i.e. only when my app installs for the first time on a device).
I googled for help and some people suggested to insert the data in the
onCreate() method of the SQLiteOpenHelper, but records couldn't be inserted using that method.
This is my code
public class EventsData extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "events.db";
private static final int DATABASE_VERSION = 1;
// Table name
public static final String TABLE = "events";
// Columns
public static final String NAME = "first";
public static final String TITLE = "second";
public EventsData(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE "+TABLE+"(" + _ID + "INTEGER PRMARY KEY AUTOINCREMENT, " + NAME +"TEXT, " + TITLE + "TEXT NOT NULL);");
db.execSQL("INSERT INTO " + TABLE +" VALUES (1,'david','packard')");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE);
onCreate(db);
}
}