首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不能将数据插入sqlit数据库表中。

不能将数据插入sqlit数据库表中。
EN

Stack Overflow用户
提问于 2012-05-12 21:01:54
回答 2查看 158关注 0票数 0

我试图在sqlit数据库中插入数据,但是我得到了android SQLiteConstraintException: error代码19:约束失败异常。我在这个主题中看到了大量的问题,我已经阅读并尝试过这些问题,但是这个异常仍然存在,我不知道这个异常是否是由自动增量food_id值引起的,因为insert语句返回-1,我想知道为什么这个异常发生在第一个insert id正确完成但后面的所有插入都失败了,为什么会发生这个错误,以及如何解决它?请帮帮我..。

DBAdapter类中的create语句

代码语言:javascript
复制
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) );" ;

插入方法

代码语言:javascript
复制
// 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代码

代码语言:javascript
复制
 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();

这是Log.e行的结果(“标记”,food_id+“食品插入已完成”);在我的日志中是-1

mylog

代码语言:javascript
复制
   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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-05-13 07:16:30

尝试删除所有(非空)约束,并保存空食物。如果正确保存,请逐个添加约束(不是NULL)。

我认为其中一个值被传递为NULL。

票数 0
EN

Stack Overflow用户

发布于 2012-05-12 21:21:43

error.means are.violating是一个约束(很明显)。很可能保留“null”列为null。

还可以尝试多次保存相同的组合,从而违反了主键。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10567384

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档