Почему SQLite db.insert () всегда возвращает -1? - PullRequest
0 голосов
/ 28 марта 2020

Почему SQLite db.insert() всегда возвращает -1 в Android Studio?

Когда я использую функцию insertData, она всегда возвращает -1. Я попробовал все, но не могу найти решение.

Мой onCreate метод приведен ниже: -

public void onCreate(SQLiteDatabase sqLiteDatabase) {

        String sub_string1 = COL_B +" TEXT PRIMARY KEY, "+SUB_COL_1 +" INTEGER, "+SUB_COL_2 +" INTEGER, "+SUB_COL_3 +" INTEGER, "+SUB_COL_4 +" INTEGER, ";
        String sub_string2 = SUB_COL_5 +" INTEGER, "+SUB_COL_6 +" INTEGER, "+SUB_COL_7 +" INTEGER, "+SUB_COL_8 +" INTEGER, ";
        String sub_string3 = SUB_COL_9 +" INTEGER, "+SUB_COL_10+" INTEGER, "+SUB_COL_11+" INTEGER, "+SUB_COL_12+" INTEGER, ";
        String sub_string4 = SUB_COL_13+" INTEGER, "+SUB_COL_14+" INTEGER, "+SUB_COL_15+" INTEGER, "+SUB_COL_16+" INTEGER, ";
        String sub_string5 = SUB_COL_17+" INTEGER, "+SUB_COL_18+" INTEGER, "+SUB_COL_19+" INTEGER, "+SUB_COL_20+" INTEGER, ";
        String sub_string6 = SUB_COL_21+" INTEGER, "+SUB_COL_22+" INTEGER, "+SUB_COL_23+" INTEGER, "+SUB_COL_24+" INTEGER, "+COL_E+" INTEGER " ;
        String string = "("+sub_string1+sub_string2+sub_string3+sub_string4+sub_string5+sub_string6+")";

        sqLiteDatabase.execSQL("create table "+ TABLE_NAME + string );
    }

Вот моя функция insertData: -

public boolean insertData(int mother,int child,int mal_child){

    Column Beneficiary,Cost_of_egg,Cost_of_soyabean,Cost_of_vegetable,Cost_of_potato,Cost_of_banana;
    Beneficiary = new Column(mother,child,mal_child);
    Cost_of_egg = new Column(mother,child,mal_child,2,1);
    Cost_of_soyabean = new Column(mother,child,mal_child,2,1);
    Cost_of_vegetable = new Column(mother,child,mal_child,2,1);
    Cost_of_potato = new Column(mother,child,mal_child,2,1);
    Cost_of_banana = new Column(mother,child,mal_child,2,1);

    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(SUB_COL_1,Beneficiary.getMother());
    contentValues.put(SUB_COL_2,Beneficiary.getSev_mal_children());
    contentValues.put(SUB_COL_3,Beneficiary.getNormal_and_mod_mal());
    contentValues.put(SUB_COL_4,Beneficiary.getTotal());

    contentValues.put(SUB_COL_5,Cost_of_egg.getMother());
    contentValues.put(SUB_COL_6,Cost_of_egg.getSev_mal_children());
    contentValues.put(SUB_COL_7,Cost_of_egg.getNormal_and_mod_mal());
    contentValues.put(SUB_COL_8,Cost_of_egg.getTotal());

    contentValues.put(SUB_COL_9,Cost_of_soyabean.getMother());
    contentValues.put(SUB_COL_10,Cost_of_soyabean.getSev_mal_children());
    contentValues.put(SUB_COL_11,Cost_of_soyabean.getNormal_and_mod_mal());
    contentValues.put(SUB_COL_12,Cost_of_soyabean.getTotal());

    contentValues.put(SUB_COL_13,Cost_of_vegetable.getMother());
    contentValues.put(SUB_COL_14,Cost_of_vegetable.getSev_mal_children());
    contentValues.put(SUB_COL_15,Cost_of_vegetable.getNormal_and_mod_mal());
    contentValues.put(SUB_COL_16,Cost_of_vegetable.getTotal());

    contentValues.put(SUB_COL_17,Cost_of_potato.getMother());
    contentValues.put(SUB_COL_18,Cost_of_potato.getSev_mal_children());
    contentValues.put(SUB_COL_19,Cost_of_potato.getNormal_and_mod_mal());
    contentValues.put(SUB_COL_20,Cost_of_potato.getTotal());

    contentValues.put(SUB_COL_21,Cost_of_banana.getMother());
    contentValues.put(SUB_COL_22,Cost_of_banana.getSev_mal_children());
    contentValues.put(SUB_COL_23,Cost_of_banana.getNormal_and_mod_mal());
    contentValues.put(SUB_COL_24,Cost_of_banana.getTotal());

    contentValues.put(COL_B,"3.10.19");
    contentValues.put(COL_E,Cost_of_banana.getTotal()+Cost_of_egg.getTotal()+Cost_of_potato.getTotal()+Cost_of_soyabean.getTotal()+Cost_of_vegetable.getTotal());

    long success = db.insert(TABLE_NAME,null,contentValues);
    Log.d("Values of success = ", "******************* " + success + " ***************");
    if(success==-1)
        return false;
    else
        return true;
}
...