That is main activity oncreate
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DBHandler db = new DBHandler(this);
db.addEvent(new Event("Ravi", "f"));
Log.d(TAG, "Inserting ..");
}
}
That is database handler class oncreate
public void onCreate(SQLiteDatabase db) {
Log.d("Chic","oncreta");
String CREATE_EVENTS_TABLE = "CREATE TABLE If not exists " + TABLE_EVENTS + "("
+ KEY_ID + " INTEGER PRIMARY KEY autoincrement," + KEY_NAME + " TEXT not null,"
// + KEY_LOCATION + " TEXT not null," + KEY_TYPE + " TEXT,"
+ KEY_START_TIME + " TEXT not null"
// + KEY_END_TIME + " TEXT not null,"
// + KEY_DESCRIPTION + " TEXT not null"
+ ")";
db.execSQL(CREATE_EVENTS_TABLE);
}
statics:
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "eventsManager";
// events table name
private static final String TABLE_EVENTS = "events";
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_START_TIME = "start_time";
and add method
public void addEvent(Event event) {
SQLiteDatabase db = this.getWritableDatabase();
//db.execSQL("DROP TABLE IF EXISTS "+TABLE_EVENTS);
ContentValues values = new ContentValues();
values.put("name", "f"); // event Name
values.put("start_time", "g"); // Event start
// Inserting Rows
db.insert(TABLE_EVENTS, null, values);
db.close(); // Closing database connection
}
I dont add thing coming from main. I am trying to understand and figure out. For one hour, i tried to drop table. I dropped it. Now i cant create it. COde is same. It is creating new Dbhandler in mainoncreate but it doesnot go into oncreate method. IT just go to add method when called in main. But there is no table. I dont understand why it doesnot create if there is no table.
Error is that
1 (1) no such table: events
/SQLiteDatabase: Error inserting start_time=g name=f
android.database.sqlite.SQLiteException: no such table: events (code 1): , while compiling: INSERT INTO events(start_time,name) VALUES (?,?)
Logs shows only one inserting from main. Not other, oncreate.
Android SQLiteOpenHelper : onCreate() method is not called. Why?
i looked here but they couldnot help me.
db.getWritableDatabase();
I added this , i saw from there but still same.
Aucun commentaire:
Enregistrer un commentaire