Search code examples
androidandroid-sqlitesqliteopenhelper

SQLite Datebase Creation


I am trying to create a database, but nothing happening with my code. I know this question is very basic but I am a new learner so I could not solve my problem so kindly help me.

public class DatabaseHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME="employeedb";
    private static final String TABLE_NAME="EMPLOYEETABLE";
    private static final int DATABASE_VERSION=1;
    private static final String EID="_id";
    private static final String NAME="Name";
    private static final String CREATE_TABLE="CREATE TABLE "+TABLE_NAME+" ("+EID+" INTEGER PRIMARY AUTOINCREMENT, "+NAME+" VARCHAR(255))";
    private static final  String DROP_TABLE="DROP TABLE "+TABLE_NAME+"IF EXISTS";
    private Context context;
    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        this.context = context;
        Message.message(context, "Constructor called.");
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        try {
            Message.message(context, "onCreate called.");
            db.execSQL(CREATE_TABLE);
        }
        catch (Exception e)
        {
            Message.message(context, ""+e);
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        try {
            Message.message(context, "onUpgrade called.");
            db.execSQL(DROP_TABLE);
            onCreate(db);
        } catch (SQLException e) {
            Message.message(context, ""+e);
        }
    }
}

Here is Message Class that only one method

public class Message {
    public static void message(Context context, String message){
        Toast.makeText(context,message,Toast.LENGTH_SHORT);

    }
}

Here is MainActivity Code where i call the getWritabeDatase Method.

public class MainActivity extends Activity implements MyDialog.Communicator {
    LinearLayout layout;
    TextView txt;
    TextView date;
    DatabaseHelper dbhelper;
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        layout = (LinearLayout) findViewById(R.id.layoutbottom);
        date = (TextView) findViewById(R.id.date);
        txt =(TextView) findViewById(R.id.title);
        GeneralMethods generalmethods = new GeneralMethods(this,this);
        dbhelper = new DatabaseHelper(this);
        SQLiteDatabase sqLiteDatabase = dbhelper.getWritableDatabase();
    }
    public void showDialog(View v){
        FragmentManager manager = getFragmentManager();
        MyDialog myDialog = new MyDialog();
        myDialog.show(manager,"MyDialog");
    }
    @Override
    public void onDialogueMessage(String message) {
        date.setText(message);
    }
}

Solution

  • You onCreate will get called when you first access the database using the helper.

    Any call to either

    getReadableDatabase() or getWritableDatabase()
    

    will cause the onCreate to get triggered and only then. Your onUpgrade will trigger when you change the database version of an already existing schema.

    Also as per SQLite documentation, an integer primary key will automatically get incremented to the max+1 value on it's own.

    Adding an Auto increment causes unnecessary explicit overhead and changes the rowid selection algorithm and they do not recommend it.

    You have forgotten the show() for the toast in your Message class

    Toast.makeText(context,message,Toast.LENGTH_SHORT).show();
    

    Basically without the show(), the Toast will not get shown, it will just get created. Now since onCreate gets called only once, to check if it's working , uninstall and reinstall the app or clear it's data before testing