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 8925653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:45:26+00:00 2026-06-15T07:45:26+00:00

i have one table call listTable and productTable . I perform sqlite delete function

  • 0

i have one table call listTable and productTable . I perform sqlite delete function in listTable , but something wrong in my coding . Inside productTable have Foreign key reference to primary key in listTable .

SQLiteHelper.java

     public class SQLiteHelper extends SQLiteOpenHelper {
            public static final String dbName = "shoppingDB1.db";
            public static final int dbVersion = 1;
            public static final String listTable = "ShoppingList";
            public static final String listId = "ShopingList_Id";
            public static final String listName = "ShopingList_Name";

            public static final String productTable = "Product";
            public static final String product_id = "Product_Id";
            public static final String productName = "Product_Name";
            public static final String product_FId = "Product_FId";
            private static final String CREATE_SHOPPLINGLIST_TABLE = "CREATE TABLE IF NOT EXISTS "
                    + listTable
                    + " ("
                    + listId
                    + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                    + listName
                    + " TEXT ")";

            private static final String CREATE_PRODUCT_TABLE = "CREATE TABLE  IF NOT EXISTS "
                    + productTable
                    + " ("
                    + product_id
                    + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                    + productName
                    + " TEXT, "+ product_FId
                + " INTEGER NOT NULL,   "
                             FOREIGN KEY ("
                + product_FId
                + ") REFERENCES "
                + listTable
                + " ("
                + listId
                + ")
         public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_SHOPPLINGLIST_TABLE);
        db.execSQL(CREATE_PRODUCT_TABLE);
}
        public void onOpen(SQLiteDatabase db) {
        super.onOpen(db);
        if (!db.isReadOnly()) {
            // Enable foreign key constraints
            db.execSQL("PRAGMA foreign_keys=ON;");
        }
    }

ComicsData.java

public class ComicsData {
public ComicsData(Context context) {
        dbHelper = new SQLiteHelper(context);
    }

public void open() throws SQLException {
        database = dbHelper.getWritableDatabase();
    }
public void deleteList(String myid) {
        // TODO Auto-generated method stub
        String[] arg={myid};
        try{
        database.delete(SQLiteHelper.listTable, SQLiteHelper.listId+ " = ?",arg);
        }catch(Exception e){
            Log.e("cannot", e.toString());
        }
    }

error message

12-02 17:27:46.232: E/error(21668): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed

deleteList(String myid) will display error if there have product inside the list .
anyone know what is my problem ? should the foreign key problem . I passing listId to deleteList(String myid)

  • 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-15T07:45:27+00:00Added an answer on June 15, 2026 at 7:45 am

    Your products have the list id as a foreign key. You cannot delete a list which contains a product since all products which have that list id as a foreign key would then belong to a non-existent list.

    You have set constraints on (db.execSQL("PRAGMA foreign_keys=ON;");) therefore SQLite will not allow you to do this and throws the exception you are seeing.

    In other words, the constraint says that all products that have a list id must refer to an existing list.

    To fix this, you could null all productTable.listIDs that reference the list you want to delete. To do this automatically, add ON DELETE SET NULL to your constraint.

    You should read this:

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

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

Sidebar

Related Questions

I have one table of items, let's call it ITEMS. Now I need to
i have one table, let's call it 'TBL'. i have one column that have
I have two tables, one table is called queuelist and the other is call
I actually don't even know how to call this :P, but... I have one
I have one table which contains events and dates, and another which contains the
I have one table that has two fields - ID1 and ID2 ID1 can
I have one table having ID and other attributes. How can I get list
I have one table that has sales records and another table that has additional
I have one table, t1, which has fileds called userid, week and year fields.
I have one table containing user sessions and another to indicate violations in the

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.