0

I can't put elements in my table because my table is never created. I do call getWritableDatabase() in my helper constructor so I don't understand. Thanks in advance.

Here is the code :

 public class DatabaseAdapter {

DatabaseHelper helper ;
Context context;

public DatabaseAdapter(Context context){
    helper = new DatabaseHelper(context);
    this.context=context;
}

public long insertData(String title, String login, String password){

    ContentValues contentValues = new ContentValues();
    contentValues.put(helper.COL_TITLE,title);
    contentValues.put(helper.COL_LOGIN,login);
    contentValues.put(helper.COL_PASSWORD,password);

    long result = helper.db.insert(helper.TABLE_NAME,null,contentValues);
    return result;
}

static class DatabaseHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "PassDB";
    private static final String TABLE_NAME = "Passwords";
    private static final String UID = "_id";
    private static final String COL_TITLE = "title";
    private static final String COL_LOGIN = "login";
    private static final String COL_PASSWORD = "password";
    private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " ("
            + UID + " INTEGER PRIMARY KEY AUTO INCREMENT,"
            + COL_TITLE + " VARCHAR(255)," + COL_LOGIN + " VARCHAR(255),"
            + COL_PASSWORD + " VARCHAR(255));";
    private static final String DROP_TABLE = "DROP TABLE IF EXISTS" + TABLE_NAME + ";";
    private static final int DATABASE_VERSION = 4;
    Context c;
    SQLiteDatabase db;

    public DatabaseHelper(Context context) {

        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        c=context;
        Toast.makeText(c, "constructor called", Toast.LENGTH_SHORT).show();
        db = getWritableDatabase();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        try {
            db.execSQL(CREATE_TABLE);
            Toast.makeText(c, "onCreate succesfull", Toast.LENGTH_SHORT).show();

        } catch (Exception ignore) {
            Toast.makeText(c, "onCreate not succesfull", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        try {
            db.execSQL(DROP_TABLE);
            onCreate(db);
        } catch (Exception ignore) {
        }
    }
}

}

and the function in the activity which supposedly calls this.

public void save(View v) {
    titleED = (EditText) findViewById(R.id.editText);
    loginED = (EditText) findViewById(R.id.editText2);
    passwordED = (EditText) findViewById(R.id.editText3);

    String title = changeNullToEmptyString(titleED.getText().toString());
    String login = changeNullToEmptyString(loginED.getText().toString());
    String password = changeNullToEmptyString(passwordED.getText().toString());


    DatabaseAdapter databaseAdapter = new DatabaseAdapter(this);
    long result = databaseAdapter.insertData(title, login, password);
    if(result>0){
        Toast.makeText(this,title + "was saved succesfully", Toast.LENGTH_SHORT).show();
    }
    else{
        Toast.makeText(this,"An error occured, the password was not saved. result ="+result, Toast.LENGTH_SHORT).show();
    }
}

The toasts in onCreate never appear.

Ced
  • 15,847
  • 14
  • 87
  • 146

0 Answers0