Home:ALL Converter>SQLite foreign key mismatch on insert Command

SQLite foreign key mismatch on insert Command

Ask Time:2016-03-27T18:00:58         Author:Danyal Ahmed Chaudhry

Json Formatter

03-27 10:39:55.279 23303-23303/com.example.danyalahmed.stockmanagement E/SQLiteLog: (1) foreign key mismatch - "Product" referencing "Category" 03-27 10:39:55.280 23303-23303/com.example.danyalahmed.stockmanagement E/SQLiteDatabase: Error inserting Code=2536 Price=5 CategoryID=1 Quantity=2 Name=Olá android.database.sqlite.SQLiteException: foreign key mismatch - "Product" referencing "Category" (code 1): , while compiling: INSERT INTO PRODUCT(Code,Price,CategoryID,Quantity,Name) VALUES (?,?,?,?,?) at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:895) at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:506) at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) at android.database.sqlite.SQLiteProgram.(SQLiteProgram.java:58) at android.database.sqlite.SQLiteStatement.(SQLiteStatement.java:31) at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469) at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341) at com.example.danyalahmed.stockmanagement.Classes.DbAdapter.insertData(DbAdapter.java:131) at com.example.danyalahmed.stockmanagement.Activities.Scan_Page$3.onClick(Scan_Page.java:162) at android.view.View.performClick(View.java:4856) at android.view.View$PerformClick.run(View.java:19956) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:211) at android.app.ActivityThread.main(ActivityThread.java:5373) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)

if you check this,

Error inserting Code=2536 Price=5 CategoryID=1 Quantity=2 Name=Olá

you can see that I have the CategoryID = 1.

when i create the DB i put there the categories:

INSERT INTO Category VALUES(1, 'OTHERS');
INSERT INTO Category VALUES(2, 'Crisp');
INSERT INTO Category VALUES(3, 'Sweet');

here is the product structure:

CREATE TABLE IF NOT EXISTS Product (" +
      " _id INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL  UNIQUE," +
      "Name VARCHAR NOT NULL," +
      "Code VARCHAR NOT NULL," +
      "Quantity INTEGER NOT NULL," +
      "PRICE DOUBLE NOT NULL," +
      "CategoryID INTEGER NOT NULL," +
      "FOREIGN KEY(CategoryID) REFERENCES Category(ID));

and here is the query:

public boolean insertData(String Table, String[] Columns, String[] Values) {   
    db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    for (int i = 1; i < Columns.length; i++) {
        contentValues.put(Columns[i].replace(Table + ".", ""), Values[i].trim());
    }
    return ((db.insert(Table, null, contentValues)) != -1);
}

any help is appreciated.

Thanks.

Author:Danyal Ahmed Chaudhry,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/36246077/sqlite-foreign-key-mismatch-on-insert-command
yy