我创建了一个SQLite Trigger,它应该在数据已经插入数据库后选择我的reg_bttn onCLick时执行。我没有编译错误,没有logcat错误,没有触发器阻止新插入。我的触发器语句是找到我的DB类。它应该在我的班级中进行插入。
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();
答案 0 :(得分:0)
我在使用触发器中的计数与检查null时取得了成功。例如:( SELECT Count()FROM表WHERE new.xxx = table.xxx)== 0)。