2012-05-12 17 views
0

我想插入數據在我的sqlit數據庫,但我得到了android SQLiteConstraintException:錯誤代碼19:約束失敗的異常。我看到在這個主題中有很多問題,我已經閱讀並嘗試了一堆,但是仍然存在異常,我想知道自動增加food_id值引起的異常,因爲插入語句返回-1,而我不知道爲什麼自第一次插入id正確完成後發生這種異常,但所有後面的插入都失敗了,爲什麼會發生此錯誤,以及我如何解決它?請幫助我..數據無法插入sqlit數據庫表

在將對DBAdapter類的創建語句

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));" ; 

插入方法

// 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代碼

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(「tag」,food_id +「食物插入完成」);在我的日誌是-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 

回答

0

嘗試刪除所有(非空)約束,並保存空的食物。 如果保存正確,請嘗試逐個添加約束(NOT NULL)。

我認爲其中一個值作爲NULL傳遞。

+0

是的食物表中的主鍵,我第一次嘗試將數據插入到它創建的食物表中,並具有自動值,但在下一次它的值始終爲零,並沒有插入成功,爲什麼?是我用來插入自動增量主鍵不正確的方式? – user

+0

嘗試將主鍵ID從_id更改爲其他內容,可能是由android本身使用....也許 –

0

這error.means你are.violating約束(顯然)。最有可能留下「非空」列爲空。

您也可能通過嘗試多次保存同一組合而違反了主鍵。

+0

但我插入表中的所有值,食物表我插入除food_id(_id)以外的所有值,因爲它的自動增量 – user

+0

請幫助我.... – user

+0

更新我的答案與另一個可能的違反約束。 – Barak