Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7817551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:15:43+00:00 2026-06-02T06:15:43+00:00

I have the following database in Android: Table A: [_id, value1] Table B: [_id,

  • 0

I have the following database in Android:

Table A: [_id, value1]
Table B: [_id, fk_tablea, value2]

With code:

db.execSQL("CREATE TABLE IF NOT EXISTS table_A ( "
        + "_id long primary key , value1 long  );");
db.execSQL("CREATE TABLE IF NOT EXISTS table_B ( "
        + "_id long primary key ,fk_tablea long  , value2 long,"
        + "FOREIGN KEY (fk_tablea) REFERENCES table_A (value1) ON DELETE CASCADE);");

And if I make (with the table empty):

ContentValues values= new ContentValues();
values.put("_id",1);
values.put("value1",900);
mDb.replace("table_A", null, values);

or

mDb.execSQL("INSERT OR REPLACE  INTO table_A(_id,value1) VALUES (1,900)");

of

mDb.insertWithOnConflict("table_A", null, values, SQLiteDatabase.CONFLICT_REPLACE);

It crashes with an exception Caused by: android.database.sqlite.SQLiteException: foreign key mismatch: , while compiling: INSERT OR REPLACE INTO table_A(_id,value1) VALUES (2,900)
. But if instead of replace I use insert

ContentValues values= new ContentValues();
values.put("_id",1);
values.put("value1",900);
mDb.insert("table_A", null, values);

Works correctly, why is that happening? and how can I solve it? Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T06:15:44+00:00Added an answer on June 2, 2026 at 6:15 am

    The foreign key in table_B refers to a non-unique field value1 in table_A. The following demonstrates how that cannot work:

    sqlite> pragma foreign_keys=1;
    sqlite> create table foo (a int, b int);
    sqlite> create table bar (c int, d int, foreign key (c) references foo(b));
    sqlite> insert into foo values (1,1);
    sqlite> insert into bar values (1,1);
    Error: foreign key mismatch
    sqlite> create unique index idx1 on foo(b);
    sqlite> insert into bar values (1,1);
    sqlite> 
    

    Documentation this way:

    http://www.sqlite.org/foreignkeys.html#fk_indexes

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have the following database table: record_id | record_date | record_value -----------+-------------+--------------
I have made the following code to retrive data from SQLite database. public Cursor
I have the following code that creates my table. I insert data into it
I want to populate a list from my database. I have the following code:
In PHP, I have following Singleton Database Class: class Database { private static $instance;
I have the following criteria Database should be protected with a username and password.
I have the following MySQL database: |deal_name|price|shop|expiration_date - - - - - - -
I have created the following database layout, and started coding the application. The more
I have the following (simplified) database tables: Products - ProductID, int, PK - Name,
I have following data to save in database using php my data is :

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.