I have error on my foreign key.. Anyone can help me figure out on how to solve 
this problem.

//THIS IS MY ONCREATE METHOD

@Override
public void onCreate(SQLiteDatabase db) {

    // TABLE FOR USER
     String CREATE_TABLE = "CREATE TABLE " + TABLE_REGISTER + "("
            + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
            + KEY_NAME + " TEXT NOT NULL,"
            + KEY_USER_NAME + " TEXT NOT NULL,"
            + KEY_MOB_NO + " TEXT NOT NULL,"
            + KEY_PASSWORD + " TEXT NOT NULL" + ");";

    // TABLE FOR ALLOWANCE
     String CREATE_TABLE_ALLOWANCE = "CREATE TABLE " + TABLE_ALLOWANCE + "("
            + ALLOWANCE_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
            + USER_ALLOW_ID + " INTEGER NOT NULL,"
            + ALLOWANCE_AMOUNT + " INTEGER NOT NULL,"
            + " FOREIGN KEY ("+USER_ALLOW_ID+") REFERENCES 
"+TABLE_REGISTER+"("+KEY_ID+"));";

    //TRIGGER
    String CREATE_TRIGGER_ALLOWUSER = "CREATE TRIGGER " + TRIGGER_USERALLOW +
            " BEFORE INSERT " + " ON " + TABLE_ALLOWANCE +
            " FOR EACH ROW BEGIN" + " SELECT CASE WHEN ((SELECT " + KEY_ID + " 
FROM "
            + TABLE_REGISTER + " WHERE " + KEY_ID + "=new." + USER_ALLOW_ID + " 
) IS NULL)"
            + " THEN RAISE (ABORT, 'Foreign Key Violation') END;" + " END;";


    db.execSQL(CREATE_TABLE);
    db.execSQL(CREATE_TABLE_ALLOWANCE);
    db.execSQL(CREATE_TRIGGER_ALLOWUSER);
    //db.execSQL("PRAGMA foreign_keys = ON;");
}


@Override
public void onConfigure(SQLiteDatabase database) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        database.setForeignKeyConstraintsEnabled(true);
    } else {
        database.execSQL("PRAGMA foreign_keys=ON");
    }
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub
    // Drop older table if existed
    Log.w(DataBaseHandler.class.getName(), "Upgrading database from version " + 
oldVersion + " to "
    + newVersion + ", which will destroy all old data");
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_REGISTER);
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_ALLOWANCE);
    db.execSQL("DROP TABLE IF EXISTS " + TRIGGER_USERALLOW);
    // Create tables again
    onCreate(db);
}




//THIS IS MY LOGCAT

10-18 02:53:25.838 20439-20439/com.example.maecea.ipon2 E/SQLiteDatabase: Error 
inserting allowance_amount=300
                                                                          
android.database.sqlite.SQLiteConstraintException: Foreign Key Violation (code 
19)
                                                                              
at 
android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native
 Method)
                                                                              
at 
android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:775)
                                                                              
at 
android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
                                                                              
at 
android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
                                                                              
at 
android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
                                                                              
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
                                                                              
at 
com.example.maecea.ipon2.DataBaseHandler.addallowance(DataBaseHandler.java:143)
                                                                              
at com.example.maecea.ipon2.TypeWeekly.OnProceedMonthlyClick(TypeWeekly.java:40)
                                                                              
at java.lang.reflect.Method.invokeNative(Native Method)
                                                                              
at java.lang.reflect.Method.invoke(Method.java:511)
                                                                              
at 
android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
                                                                              
at android.view.View.performClick(View.java:4084)
                                                                              
at android.view.View$PerformClick.run(View.java:16966)
                                                                              
at android.os.Handler.handleCallback(Handler.java:615)
                                                                              
at android.os.Handler.dispatchMessage(Handler.java:92)
                                                                              
at android.os.Looper.loop(Looper.java:137)
                                                                              
at android.app.ActivityThread.main(ActivityThread.java:4745)
                                                                              
at java.lang.reflect.Method.invokeNative(Native Method)
                                                                              
at java.lang.reflect.Method.invoke(Method.java:511)
                                                                              
at 
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
                                                                              
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
                                                                              
at dalvik.system.NativeStart.main(Native Method)


-- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/android-developers/af0cb3c9-8a20-4f81-8561-1f1b69c79b99%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to