0

我创建了一个 SQLite 触发器,当我的 reg_bttn onCLick 在数据已插入数据库后被选中时,它应该执行。我没有编译错误,没有 logcat 错误,也没有触发器阻止新插入。我的触发器语句位于我的数据库类中。是否应该在我的班级中进行插入。

  public LoginDB(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub      

        String sqlDataStore = "create table if not exists " +
        TABLE_NAME_CREDENTIALS + " ("+ BaseColumns._ID + " integer primary key autoincrement,"

                    + COLUMN_NAME_USERNAME + " text not null,"
                    + COLUMN_NAME_PASSWORD + " text not null);";

        db.execSQL("CREATE TRIGGER DATABASE_NAME " +
                " BEFORE INSERT " +
                " ON "+TABLE_NAME_CREDENTIALS+ 
                " WHEN  BEGIN " +  
                " SELECT (( SELECT * FROM " + TABLE_NAME_CREDENTIALS + " WHERE "
                        + COLUMN_NAME_USERNAME + "' AND "+ COLUMN_NAME_PASSWORD +") IS NOT NULL)" +
                        " THEN RAISE ( ABORT, 'ID ALREADY EXIST') END;" +
                        "END;");


           db.execSQL(sqlDataStore);
    }

插入:

      public void onClick (View v) {

    rUsername = rName.getText().toString();
    rPasscode = rCode.getText().toString();

    RegDetails regDetails = new RegDetails();
    regDetails.setrName(bundleRegName);
    regDetails.setpCode(bundleRegCode);
    if(v.getId()==R.id.rtn_button){
        finish();

    }else if(v.getId()==R.id.reg_button){
        insertCredentials(regDetails);
    }
}
    private void insertCredentials(RegDetails regDetails){

        LoginDB androidOpenDBHelper = new LoginDB(this);
        SQLiteDatabase sqliteDB = androidOpenDBHelper.getWritableDatabase();

        ContentValues contentValues = new ContentValues();
        contentValues.put(LoginDB.COLUMN_NAME_USERNAME, rUsername);
        contentValues.put(LoginDB.COLUMN_NAME_PASSWORD, rPasscode);

        long affectedColumnid = sqliteDB.insert(LoginDB.TABLE_NAME_CREDENTIALS, null, contentValues);

        Toast.makeText(getApplicationContext(), "Credentials Saved! Please login" + affectedColumnid, Toast.LENGTH_SHORT).show();
        sqliteDB.close();
        finish();
4

1 回答 1

0

我在触发器中使用计数与检查 null 方面取得了成功。例如:(SELECT Count() FROM table WHERE new.xxx = table.xxx) == 0)。

于 2012-12-09T04:00:03.320 回答