0

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.

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

but it is not being called even at first time
your code toastmessage.showtoast(context, "oncreate called"); is in catch exception block, move it into the try block?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.