onCreate没有在SQLiteHelper数据库中调用

时间:2015-03-15 17:40:32

标签: java android sqlite

我无法在我的表中放置元素,因为我的表永远不会被创建。我在我的帮助器构造函数中调用getWritableDatabase(),所以我不明白。提前谢谢。

以下是代码:

 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) {
        }
    }
}

}

以及可能称之为活动的活动中的函数。

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();
    }
}

onCreate中的祝酒词永远不会出现。

0 个答案:

没有答案