简体   繁体   中英

Why data not inserting to local database in android

These are the fields in table.

public static String Table_Train_Demo = "SMS_TRAIN_DEMO";
public static String Train_DEMO_ID = "TRAIN_DEMO_ID";
public static String Train_Person_Name = "PERSON_NAME";
public static String Train_Designation = "DESIGNATION";
public static String Train_Status = "STATUS";
public static String Train_Station_Or_Place_ID = "STATION_OR_PLACE_ID";
public static String Train_Discussion_ID = "DISCUSSION_ID";
public static String Train_EMP_Status_ID = "EMP_STATUS_ID";
public static String Train_Demo_Plan_ID = "DEMO_PLAN_ID";
public static String Train_APP_Plan_ID = "APP_PLAN_ID";
public static String Train_Pk = "SMS_TRAIN_DEMO_pk";
public static String Train_Constraint = 
"SMS_TRAIN_DEMO_SMS_EMP_DAILY_STATUS";
public static String Train_Plan_Constraint = 
"SMS_TRAIN_DEMO_SMS_PLAN_TRAIN_DEMO";
public static String Train_App_Plan_Constraint = 
"SMS_TRAIN_DEMO_SMS_PLAN_APP";

This is the create statement of the table

private static final String Create_Table_Train_Demo = "CREATE TABLE " + 
Table_Train_Demo + " ("
        + Train_DEMO_ID + " INTEGER NOT NULL CONSTRAINT " + Train_Pk + " 
PRIMARY KEY AUTOINCREMENT, "
        + Train_Person_Name + " TEXT NOT NULL, "
        + Train_Designation + " TEXT NOT NULL, "
        + Train_Status + " INTEGER NOT NULL, "
        + Train_Station_Or_Place_ID + " INTEGER ,"
        + Train_Discussion_ID + " INTEGER NOT NULL ,"
        + Train_EMP_Status_ID + " INTEGER NOT NULL ,"
        + Train_Demo_Plan_ID + " INTEGER NOT NULL ,"
        + Train_APP_Plan_ID + " INTEGER NOT NULL ,"
        + " CONSTRAINT " + Train_Constraint + " FOREIGN KEY (" + 
Train_EMP_Status_ID + ")"
        + " REFERENCES " + Table_Daily_EmpStatus + "(" + EStatus_ID + ")"
        + " CONSTRAINT " + Train_Plan_Constraint + " FOREIGN KEY (" + 
Train_Demo_Plan_ID + ")"
        + " REFERENCES " + Table_Plan_Train_Demo + "(" + PTrain_Demo_Plan_ID 
+ ")"
        + " CONSTRAINT " + Train_App_Plan_Constraint + " FOREIGN KEY (" + 
Train_APP_Plan_ID + ")"
        + " REFERENCES " + Table_Plan_App + "(" + PAPP_Plan_ID + ")"
        + ")";

Here I am trying to insert values to the table.

public boolean addTrngDemoEntryRecord(TRN_DEMOS recInfo, long StatusEntryID, 
long PID) {
    SQLiteDatabase db = null;
    try {
        db = this.getWritableDatabase();
        if (db.isOpen()) {
            ContentValues values = new ContentValues();
            if (recInfo != null) {
                values.put(Train_Demo_Plan_ID,PID);
                values.put(Train_APP_Plan_ID,PID);
                values.put(Train_EMP_Status_ID,StatusEntryID);
                values.put(Train_Person_Name, recInfo.PNAME);
                values.put(Train_Designation, recInfo.DESG);
                values.put(Train_Status, recInfo.STATUS);
                values.put(Train_Discussion_ID, recInfo.DID);
                values.put(Train_Station_Or_Place_ID,0);
            }
            long row_id = db.insert(Table_Train_Demo, null, values);
            if (row_id == -1) {
                return false;
            } else {
                return true;
            }
        } else
            return false;
    }catch (Exception e) {
        Log.d("eEmp/TrngDemoplan", "Excdeption Occurred due to " + 
e.toString());
        return false;
    }  finally {
        if (db != null)
            db.close();
    }
}

Noexceptions are coming while table creation.

But while inserting data into the table

long row_id = db.insert(Table_Train_Demo, null, values);

row_id = -1 is coming and returns false value.

what might be the problem. I am new to android.

Any help would be appreciated.

log these values recInfo.PNAME, recInfo.DESG, recInfo.STATUS and recInfo.DID
as these values can't be null.
for a try just used hardcoded values 
  values.put(Train_Person_Name, "xyz");
                values.put(Train_Designation, "xyz");
                values.put(Train_Status, "xyz");
                values.put(Train_Discussion_ID,"xyz");

and check the value of row_id. 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM