为什么数据没有插入android中的本地数据库

时间:2018-02-06 05:45:59

标签: java android sqlite

这些是表格中的字段。

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

这是表

的create语句
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 + ")"
        + ")";

这里我试图将值插入表中。

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();
    }
}

创建表时会出现无异常。

但是在将数据插入表格时

  

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

row_id = -1即将到来且returns false值。

可能是什么问题。我是android的新手。

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

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.