Reputation: 512
i am learning sqlite from slidenerd. Following all as mention in tutorials but getting issue.
Constructor is working properly but oncreate is not working.
Openhelper class code
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class Kamisqlhelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME="kamidatabase";
public static final String TABLE_NAME="kamitable";
public static final int DATABASE_VERSION=1;
private static final String id="_id";
private static final String names="Name";
private static final String contact="PhoneNo";
private static final String CREATE_TABlE = "create table "+TABLE_NAME+"("+id+" integer primary key autoincrement, "+names+" varchar(255), "+contact+" integer);";
private static final String droptable= "drop table "+TABLE_NAME+" if exists";
private Context context;
public Kamisqlhelper(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context=context;
toastmessage.showtoast(context, "constructor called");
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
try {
db.execSQL(CREATE_TABlE);
} catch (SQLException e) {
// TODO Auto-generated catch block
toastmessage.showtoast(context, ""+e);
toastmessage.showtoast(context, "oncreate called");
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
try {
db.execSQL(droptable);
onCreate(db);
} catch (SQLException e) {
// TODO Auto-generated catch block
toastmessage.showtoast(context, ""+e);
toastmessage.showtoast(context, "onupgradecalled");
}
}
}
Main activity code
import android.os.Bundle;
import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
public class MainActivity extends Activity {
Kamisqlhelper kamihelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
kamihelper = new Kamisqlhelper(this);
SQLiteDatabase sqLiteDatabase = kamihelper.getWritableDatabase();
}
}
please also suggest better up to date tutorial on sqlite.
Upvotes: 0
Views: 60
Reputation: 66
onCreate
is called when the database is created for the first time, that means when you first use the SQLiteOpenHelper,it will be called, otherwise,it never be called SQLiteOpenHelper.html#onCreate
Upvotes: 1