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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:34:57+00:00 2026-06-03T20:34:57+00:00

I am trying to insert data in my sqlit database but I got android

  • 0

I am trying to insert data in my sqlit database but I got android SQLiteConstraintException: error code 19: constraint failed exception. I saw there are tons of question in this topic, I’ve read and tried a bunch of them, but the exception still , i wonder if this exception caused by the auto increment food_id value , since the insert statement return -1 , and i wonder that why this exception occur since the first insert id done correctly but all the later inserting are failed , why this error occur ,and how i can solve it? please help me..

the create statements in the DBAdapter class

private static final String Meal_TABLE_CREATE= "create table IF NOT EXISTS Meal (Date  text not null , "+
        "Time   text not null,MealType  text not null,"+ " primary key(Date,Time ,MealType) );" ;

private static final String FOOD_TABLE_CREATE= "create table IF NOT EXISTS Food (_id  INTEGER  primary key  AUTOINCREMENT  , "+
        "Food_Name   text not null,Calories  integer  not null,"+ "VB12   integer  not null,Cholesterol  integer  not null,"+ 
        "Protein   integer  not null,Iron integer  not null,Sodium integer  not null,Fat_Mono integer  not null,Fat_Sat integer  not null,carbohydrate integer  not null);" ;

private static final String MealFOOD_TABLE_CREATE= "create table IF NOT EXISTS MealFood (Date  text  not null , "+
        "Time   text not null,MealType  text not null,"+"Food_ID  integer   not null ,  primary key(Date,Time ,MealType,Food_ID) );" ;

inserting methods

// insert meal  to the meal table 
public long SaveMeal(String date , String time , String mealType)
{
    ContentValues content = new ContentValues();
    content.put(KEY_MDATE,date);
    content.put(KEY_MTIME,time);
    content.put(KEY_MEALTYPE,mealType);
    return db.insert(MEAL_TABLE_NAME, null, content);

}

// insert Food  to the Food table 
public long SaveFood(String name,int calories,int Vit_B12,int cholesterol,int protein ,int iron ,int sodium,int Fat_Mono,int Fat_Sat,int carbohydrate)
{
    ContentValues content = new ContentValues();

    content.put(KEY_FOODNAME,name);
    content.put(KEY_CALORIES,calories);
    content.put(KEY_VB12,Vit_B12);
    content.put(KEY_CHOLESTEROL,cholesterol);
    content.put(KEY_PROTEIN,protein);
    content.put(KEY_IRON,iron);
    content.put(KEY_SODIUM,sodium);
    content.put(KEY_FAT_MONO,Fat_Mono);
    content.put(KEY_FAT_Sat,Fat_Sat);
    content.put(KEY_CARBOHYDRATE,carbohydrate);


    return db.insert(FOOD_TABLE_NAME, null, content);

}

// get food id by its name   

public int getFoodIDByName(String name) throws SQLException
{   int id;
Cursor cursor = null;

try{

    cursor=db.query(true,FOOD_TABLE_NAME, new String[]{KEY_FOODID},  KEY_FOODNAME+ " = '" + name + "'", null, null, null, null,null);
    if (cursor != null) {
        cursor.moveToFirst();
    }

    id=0;
    while (cursor.moveToNext()) 
        id=cursor.getInt(cursor.getColumnIndex(KEY_FOODID));

}
finally{
    cursor.close();
    cursor.deactivate(); 
}
return id;

}


// insert mealFood   to mealFood table 
public long SaveMealFood(String date , String time , String mealType, int Food_id)
{
    ContentValues content = new ContentValues();
    content.put(KEY_MFDATE,date);
    content.put(KEY_MFTIME,time);
    content.put(KEY_MFMEALTYPE,mealType);
    content.put(KEY_MFFOODID,Food_id);
    return db.insert(MEALFOOD_TABLE_NAME, null, content);

}

java code

 DBAdapter dbAdapter=new DBAdapter(SaveMeal.this);
      dbAdapter.open();
      Food n;
     String m;
     int FoodIDByName;
     for(int i = 0; i <MealActivity.array.size(); i++){
        m=MealActivity.array.get(i).toString();
        Log.e("tag", m);//selected food name
       for (int j = 0; j < MealActivity.tempList.size(); j++){
              n=MealActivity.tempList.get(j);

              if(n.getFOOD_NAME().equals(m)){
         //save food 
 long food_id = dbAdapter.SaveFood(n.getFOOD_NAME(),n.getCALORIES(),n.getFOOD_VITAMIN_B12(),n.getCHOLESTEROL(),n.getFOOD_PROTEIN(),n.getFOOD_IRON(),n.getFOOD_SODIUM(),
 n.getFOOD_MONO_UNSATURATED_FAT(),n.getFOOD_SATURATED_FAT(),n.getFOOD_TOTAL_CARBOHYDRATE()); 
  Log.e("tag", food_id+" food inserting done");

  //save meal
 long meal_id=  dbAdapter.SaveMeal( meal_date,meal_time,Meal.MEAL_TYPE);
Log.e("tag",meal_id+" meal inserting done");

//save meal_food 
 FoodIDByName=dbAdapter.getFoodIDByName(n.FOOD_NAME);
 Log.e("tag",FoodIDByName+" food_id");
    long      meal_food_id=dbAdapter.SaveMealFood(meal_date,meal_time,Meal.MEAL_TYPE,FoodIDByName);
Log.e("tag",meal_food_id+" meal_food  inserting done");
 dbAdapter.close();

this result of this line Log.e(“tag”, food_id+” food inserting done”); in my log is -1

mylog

   Database(657):at android.database.sqlite.SQLiteStatement.native_execute(Native       Method)
   Database(657):at android.database.sqlite.SQLiteStatement.execute                 (SQLiteStatement.java:55)
   Database(657):at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1410)         
   -1 food inserting done
   18 meal inserting done 
   0 food_id
    13 meal_food inserting done
  • 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-03T20:34:59+00:00Added an answer on June 3, 2026 at 8:34 pm

    Try to remove all (Not NULL) constraints, and save the empty food.
    If it is saved properly, try to add the constraint (NOT NULL) one by one.

    I think one of the values is passed as NULL.

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

Sidebar

Related Questions

I'm trying to link sqlite3, create table and insert data but got this error:
I'm trying to insert data into the SQLite database using get EditText, but I
I was trying to insert new data int my database called KEY_DAY but when
I'm trying to insert some data in my Android SQLite freshly created Database with
i'm trying to insert some data on a database using this code: -(void)insertLocationOnDatabase:(LocationType *)aLocation
While trying to insert data to my SQl db i get the following error
I am trying to insert data in table, but if it already exists in
I’m trying to bulk insert data to SQL server express database. When doing bcp
I'm trying to insert non-latin data into sqlite database using bind variables using System.Data.SQLite.
I'm trying to make a database in android but I have this problem,erase 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.