数据库从模型插入,没有这样的列异常,

时间:2015-03-26 12:52:04

标签: java android json database gson

首先我在创建数据库方面遇到了麻烦,然后我想把我收到的巨大的json数据(我通过GsonBuilder将json转换为java模型,我看到我的实体[]列表中的所有数据,这些数据来自ReceivedEntityModel类)到数据库,但我在第一步失败。从不创建我的列,所以在插入后有0行数:(,我读了关于这个主题的所有答案,他们根本没有帮助我。我' m卡住了。任何人都可以帮忙吗?

 public void onCreate(SQLiteDatabase db)
 {

String CREATE_M_ENTITY_TABLE = "CREATE TABLE " + TABLE_2_NAME + " ("
            + rId + " INTEGER PRIMARY KEY, "
            + ParentId + " INTEGER, "
            + Code+ " INTEGER, "
            + Label + " TEXT, "
            + LabelFL + " TEXT, "
            + EntityGroupId + " INTEGER, "
            + Property + " TEXT, "
            + ReportCode+ " INTEGER, "
            + Description + " TEXT, "
            + Code1Id + " INTEGER, "
            + Code2Id + " INTEGER, "
            + Code3Id + " INTEGER, "
            + Code4Id+ " INTEGER, "
            + Code5Id + " INTEGER, "
            + DefaultDimension1+ " INTEGER, "
            + DefaultDimension2 + "INTEGER, "
            + DefaultDimension3 + " INTEGER, "
            + CompanyId + " INTEGER, "
            + BranchId + " INTEGER, "
            + ReferenceDate + " TEXT, "
            + ResField1+ " TEXT, "
            + ResField2 + " TEXT, "
            + AreaId + " INTEGER, " 
            + ModifiedDateTime + " TEXT, "
            + CreatedDateTime + " TEXT, "
            + RecId + " INTEGER, "
            + IsActive + " BIT,"
            + ModifiedBy + " INTEGER,"
            + CreatedBy + " INTEGER" + ") ";
    db.execSQL(CREATE_M_ENTITY_TABLE);
}

我在main活动中调用此函数来绑定listview

  public Cursor fetchEntities()

  {
    SQLiteDatabase db = dbHelper.getWritableDatabase();
        String myQuery="Select Code, Label from entity";

        Cursor mCursor=db.rawQuery(myQuery,null);
        int rowCount=mCursor.getCount();
    //Cursor mCursor = db.query(TABLE_2_NAME, new String[] {Code, Label }, null,null, null, null, null);

    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;
}

//如你所见,有一个类似于ReceivedEntityModel的参数,我从这个模型的getter获取数据,我想把它们放到一个数据库中,如下所示。

   public void AddNewEntity(ReceivedEntityModel[] entity)

 {
    // addnewentity 
    SQLiteDatabase db = dbHelper.getWritableDatabase();
    ContentValues values = new ContentValues();

    for (int i = 0; i < entity.length; i++) {
        values.put(rId, entity[i].getRId());
        values.put(ParentId, entity[i].getParentId());
        values.put(Code, entity[i].getCode());
        values.put(Label, entity[i].getLabel());
        values.put(LabelFL, entity[i].getLabelFL());
        values.put(EntityGroupId, entity[i].getEntityGroupId());
        values.put(ReportCode, entity[i].getReportCode());
        values.put(Description, entity[i].getDescription());
        values.put(Code1Id, entity[i].getCode1Id());
        values.put(Code2Id, entity[i].getCode2Id());
        values.put(Code3Id, entity[i].getCode3Id());
        values.put(Code4Id, entity[i].getCode4Id());
        values.put(Code5Id, entity[i].getCode5Id());
        values.put(DefaultDimension1, entity[i].getDefaultDimension1Id());
        values.put(DefaultDimension2, entity[i].getDefaultDimension2Id());
        values.put(DefaultDimension3, entity[i].getDefaultDimension3Id());
        values.put(CompanyId, entity[i].getCompanyId());
        values.put(BranchId, entity[i].getBranchId());
        values.put(ReferenceDate, entity[i].getReferenceDate());
        values.put(ResField1, (String) entity[i].getResField1());
        values.put(ResField2, (String) entity[i].getResField2());
        values.put(AreaId, entity[i].getAreaId());
        values.put(ModifiedDateTime, entity[i].getModifiedDateTime());
        values.put(CreatedDateTime, entity[i].getCreatedDateTime());
        values.put(RecId, entity[i].getRecId());
        values.put(IsActive, entity[i].getIsActive());
        values.put(ModifiedBy, entity[i].getModifiedBy());
        values.put(CreatedBy, (String) entity[i].getCreatedBy());



    }
    db.insert(TABLE_2_NAME, null, values);
    db.close(); 
}

这是Setup.java,fetchEntities(),AddNewEntity()函数从那里调用.Gson Builder让我成为一个模型。我调用AddNewEntity()

    protected String doInBackground(String... urls) {

        entityReqestInfo = new EntityReqestInfo();

        String cevap = POST_FOR_ENTITY(urls[0], entityReqestInfo);
        GsonBuilder builder = new GsonBuilder();
        Gson gson = builder.create();
        receivedEM = gson.fromJson(cevap, ReceivedEntityModel[].class);
        myDB.getDatabaseEntityOperations().AddNewEntity(receivedEM);

        pDialog.cancel();
        return cevap;

    }

以及我生成的所有实体Model ReceivedEntityModel类

   import java.util.ArrayList;
   import java.util.HashMap;
   import java.util.List;
   import java.util.Map;

       public class ReceivedEntityModel {

private Integer rId;
private Integer ParentId;
private String Code;
private String Label;
private String LabelFL;
private Integer EntityGroupId;
private String Property;
private String ReportCode;
private String Description;
private Integer Code1Id;
private Integer Code2Id;
private Integer Code3Id;
private Integer Code4Id;
private Integer Code5Id;
private Integer DefaultDimension1Id;
private Integer DefaultDimension2Id;
private Integer DefaultDimension3Id;
private Integer CompanyId;
private Integer BranchId;
private String ReferenceDate;
private String ResField1;
private String ResField2;
private String AreaId;
private String ModifiedDateTime;
private String CreatedDateTime;
private Integer RecId;
private Boolean IsActive;
private Integer ModifiedBy;
private String CreatedBy;
private String DBranch;
private String DCompany;
private String MEntityGroup;
private List<String> MEntityTableModule = new ArrayList<String>();
private Map<String, String> additionalProperties = new HashMap<String, String>();

/**
 * 
 * @return The rId
 */
public Integer getRId() {
    return rId;
}

/**
 * 
 * @param rId
 *            The rId
 */
public void setRId(Integer rId) {
    this.rId = rId;
}

public ReceivedEntityModel withRId(Integer rId) {
    this.rId = rId;
    return this;
}

/**
 * 
 * @return The ParentId
 */
public Integer getParentId() {
    return ParentId;
}

/**
 * 
 * @param ParentId
 *            The ParentId
 */
public void setParentId(Integer ParentId) {
    this.ParentId = ParentId;
}

public ReceivedEntityModel withParentId(Integer ParentId) {
    this.ParentId = ParentId;
    return this;
}

/**
 * 
 * @return The Code
 */
public String getCode() {
    return Code;
}

/**
 * 
 * @param Code
 *            The Code
 */
public void setCode(String Code) {
    this.Code = Code;
}

public ReceivedEntityModel withCode(String Code) {
    this.Code = Code;
    return this;
}

/**
 * 
 * @return The Label
 */
public String getLabel() {
    return Label;
}

/**
 * 
 * @param Label
 *            The Label
 */
public void setLabel(String Label) {
    this.Label = Label;
}

public ReceivedEntityModel withLabel(String Label) {
    this.Label = Label;
    return this;
}

/**
 * 
 * @return The LabelFL
 */
public String getLabelFL() {
    return LabelFL;
}

/**
 * 
 * @param LabelFL
 *            The LabelFL
 */
public void setLabelFL(String LabelFL) {
    this.LabelFL = LabelFL;
}

public ReceivedEntityModel withLabelFL(String LabelFL) {
    this.LabelFL = LabelFL;
    return this;
}

/**
 * 
 * @return The EntityGroupId
 */
public Integer getEntityGroupId() {
    return EntityGroupId;
}

/**
 * 
 * @param EntityGroupId
 *            The EntityGroupId
 */
public void setEntityGroupId(Integer EntityGroupId) {
    this.EntityGroupId = EntityGroupId;
}

public ReceivedEntityModel withEntityGroupId(Integer EntityGroupId) {
    this.EntityGroupId = EntityGroupId;
    return this;
}

/**
 * 
 * @return The Property
 */
public String getProperty() {
    return Property;
}

/**
 * 
 * @param Property
 *            The Property
 */
public void setProperty(String Property) {
    this.Property = Property;
}

public ReceivedEntityModel withProperty(String Property) {
    this.Property = Property;
    return this;
}

/**
 * 
 * @return The ReportCode
 */
public String getReportCode() {
    return ReportCode;
}

/**
 * 
 * @param ReportCode
 *            The ReportCode
 */
public void setReportCode(String ReportCode) {
    this.ReportCode = ReportCode;
}

public ReceivedEntityModel withReportCode(String ReportCode) {
    this.ReportCode = ReportCode;
    return this;
}

/**
 * 
 * @return The Description
 */
public String getDescription() {
    return Description;
}

/**
 * 
 * @param Description
 *            The Description
 */
public void setDescription(String Description) {
    this.Description = Description;
}

public ReceivedEntityModel withDescription(String Description) {
    this.Description = Description;
    return this;
}

/**
 * 
 * @return The Code1Id
 */
public Integer getCode1Id() {
    return Code1Id;
}

/**
 * 
 * @param Code1Id
 *            The Code1Id
 */
public void setCode1Id(Integer Code1Id) {
    this.Code1Id = Code1Id;
}

public ReceivedEntityModel withCode1Id(Integer Code1Id) {
    this.Code1Id = Code1Id;
    return this;
}

/**
 * 
 * @return The Code2Id
 */
public Integer getCode2Id() {
    return Code2Id;
}

/**
 * 
 * @param Code2Id
 *            The Code2Id
 */
public void setCode2Id(Integer Code2Id) {
    this.Code2Id = Code2Id;
}

public ReceivedEntityModel withCode2Id(Integer Code2Id) {
    this.Code2Id = Code2Id;
    return this;
}

/**
 * 
 * @return The Code3Id
 */
public Integer getCode3Id() {
    return Code3Id;
}

/**
 * 
 * @param Code3Id
 *            The Code3Id
 */
public void setCode3Id(Integer Code3Id) {
    this.Code3Id = Code3Id;
}

public ReceivedEntityModel withCode3Id(Integer Code3Id) {
    this.Code3Id = Code3Id;
    return this;
}

/**
 * 
 * @return The Code4Id
 */
public Integer getCode4Id() {
    return Code4Id;
}

/**
 * 
 * @param Code4Id
 *            The Code4Id
 */
public void setCode4Id(Integer Code4Id) {
    this.Code4Id = Code4Id;
}

public ReceivedEntityModel withCode4Id(Integer Code4Id) {
    this.Code4Id = Code4Id;
    return this;
}

/**
 * 
 * @return The Code5Id
 */
public Integer getCode5Id() {
    return Code5Id;
}

/**
 * 
 * @param Code5Id
 *            The Code5Id
 */
public void setCode5Id(Integer Code5Id) {
    this.Code5Id = Code5Id;
}

public ReceivedEntityModel withCode5Id(Integer Code5Id) {
    this.Code5Id = Code5Id;
    return this;
}

/**
 * 
 * @return The DefaultDimension1Id
 */
public Integer getDefaultDimension1Id() {
    return DefaultDimension1Id;
}

/**
 * 
 * @param DefaultDimension1Id
 *            The DefaultDimension1Id
 */
public void setDefaultDimension1Id(Integer DefaultDimension1Id) {
    this.DefaultDimension1Id = DefaultDimension1Id;
}

public ReceivedEntityModel withDefaultDimension1Id(
        Integer DefaultDimension1Id) {
    this.DefaultDimension1Id = DefaultDimension1Id;
    return this;

}

/**
 * 
 * @return The DefaultDimension2Id
 */
public Integer getDefaultDimension2Id() {
    return DefaultDimension2Id;
}

/**
 * 
 * @param DefaultDimension2Id
 *            The DefaultDimension2Id
 */
public void setDefaultDimension2Id(Integer DefaultDimension2Id) {
    this.DefaultDimension2Id = DefaultDimension2Id;
}

public ReceivedEntityModel withDefaultDimension2Id(
        Integer DefaultDimension2Id) {
    this.DefaultDimension2Id = DefaultDimension2Id;
    return this;
}

/**
 * 
 * @return The DefaultDimension3Id
 */
public Integer getDefaultDimension3Id() {
    return DefaultDimension3Id;
}

/**
 * 
 * @param DefaultDimension3Id
 *            The DefaultDimension3Id
 */
public void setDefaultDimension3Id(Integer DefaultDimension3Id) {
    this.DefaultDimension3Id = DefaultDimension3Id;
}

public ReceivedEntityModel withDefaultDimension3Id(
        Integer DefaultDimension3Id) {
    this.DefaultDimension3Id = DefaultDimension3Id;
    return this;
}

/**
 * 
 * @return The CompanyId
 */
public Integer getCompanyId() {
    return CompanyId;
}

/**
 * 
 * @param CompanyId
 *            The CompanyId
 */
public void setCompanyId(Integer CompanyId) {
    this.CompanyId = CompanyId;
}

public ReceivedEntityModel withCompanyId(Integer CompanyId) {
    this.CompanyId = CompanyId;
    return this;
}

/**
 * 
 * @return The BranchId
 */
public Integer getBranchId() {
    return BranchId;
}

/**
 * 
 * @param BranchId
 *            The BranchId
 */
public void setBranchId(Integer BranchId) {
    this.BranchId = BranchId;
}

public ReceivedEntityModel withBranchId(Integer BranchId) {
    this.BranchId = BranchId;
    return this;
}

/**
 * 
 * @return The ReferenceDate
 */
public String getReferenceDate() {
    return ReferenceDate;
}

/**
 * 
 * @param ReferenceDate
 *            The ReferenceDate
 */
public void setReferenceDate(String ReferenceDate) {
    this.ReferenceDate = ReferenceDate;
}

public ReceivedEntityModel withReferenceDate(String ReferenceDate) {
    this.ReferenceDate = ReferenceDate;
    return this;
}

/**
 * 
 * @return The ResField1
 */
public String getResField1() {
    return ResField1;
}

/**
 * 
 * @param ResField1
 *            The ResField1
 */
public void setResField1(String ResField1) {
    this.ResField1 = ResField1;
}

public ReceivedEntityModel withResField1(String ResField1) {
    this.ResField1 = ResField1;
    return this;
}

/**
 * 
 * @return The ResField2
 */
public String getResField2() {
    return ResField2;
}

/**
 * 
 * @param ResField2
 *            The ResField2
 */
public void setResField2(String ResField2) {
    this.ResField2 = ResField2;
}

public ReceivedEntityModel withResField2(String ResField2) {
    this.ResField2 = ResField2;
    return this;
}

/**
 * 
 * @return The AreaId
 */
public String getAreaId() {
    return AreaId;
}

/**
 * 
 * @param AreaId
 *            The AreaId
 */
public void setAreaId(String AreaId) {
    this.AreaId = AreaId;
}

public ReceivedEntityModel withAreaId(String AreaId) {
    this.AreaId = AreaId;
    return this;
}

/**
 * 
 * @return The ModifiedDateTime
 */
public String getModifiedDateTime() {
    return ModifiedDateTime;
}

/**
 * 
 * @param ModifiedDateTime
 *            The ModifiedDateTime
 */
public void setModifiedDateTime(String ModifiedDateTime) {
    this.ModifiedDateTime = ModifiedDateTime;
}

public ReceivedEntityModel withModifiedDateTime(String ModifiedDateTime) {
    this.ModifiedDateTime = ModifiedDateTime;
    return this;
}

/**
 * 
 * @return The CreatedDateTime
 */
public String getCreatedDateTime() {
    return CreatedDateTime;
}

/**
 * 
 * @param CreatedDateTime
 *            The CreatedDateTime
 */
public void setCreatedDateTime(String CreatedDateTime) {
    this.CreatedDateTime = CreatedDateTime;
}

public ReceivedEntityModel withCreatedDateTime(String CreatedDateTime) {
    this.CreatedDateTime = CreatedDateTime;
    return this;
}

/**
 * 
 * @return The RecId
 */
public Integer getRecId() {
    return RecId;
}

/**
 * 
 * @param RecId
 *            The RecId
 */
public void setRecId(Integer RecId) {
    this.RecId = RecId;
}

public ReceivedEntityModel withRecId(Integer RecId) {
    this.RecId = RecId;
    return this;
}

/**
 * 
 * @return The IsActive
 */
public Boolean getIsActive() {
    return IsActive;
}

/**
 * 
 * @param IsActive
 *            The IsActive
 */
public void setIsActive(Boolean IsActive) {
    this.IsActive = IsActive;
}

public ReceivedEntityModel withIsActive(Boolean IsActive) {
    this.IsActive = IsActive;
    return this;
}

/**
 * 
 * @return The ModifiedBy
 */
public Integer getModifiedBy() {
    return ModifiedBy;
}

/**
 * 
 * @param ModifiedBy
 *            The ModifiedBy
 */
public void setModifiedBy(Integer ModifiedBy) {
    this.ModifiedBy = ModifiedBy;
}

public ReceivedEntityModel withModifiedBy(Integer ModifiedBy) {
    this.ModifiedBy = ModifiedBy;
    return this;
}

/**
 * 
 * @return The CreatedBy
 */
public String getCreatedBy() {
    return CreatedBy;
}

/**
 * 
 * @param CreatedBy
 *            The CreatedBy
 */
public void setCreatedBy(String CreatedBy) {
    this.CreatedBy = CreatedBy;
}

public ReceivedEntityModel withCreatedBy(String CreatedBy) {
    this.CreatedBy = CreatedBy;
    return this;
}

/**
 * 
 * @return The DBranch
 */
public String getDBranch() {
    return DBranch;
}

/**
 * 
 * @param DBranch
 *            The D_Branch
 */
public void setDBranch(String DBranch) {
    this.DBranch = DBranch;
}

public ReceivedEntityModel withDBranch(String DBranch) {
    this.DBranch = DBranch;
    return this;
}

/**
 * 
 * @return The DCompany
 */
public String getDCompany() {
    return DCompany;
}

/**
 * 
 * @param DCompany
 *            The D_Company
 */
public void setDCompany(String DCompany) {
    this.DCompany = DCompany;
}

public ReceivedEntityModel withDCompany(String DCompany) {
    this.DCompany = DCompany;
    return this;
}

/**
 * 
 * @return The MEntityGroup
 */
public String getMEntityGroup() {
    return MEntityGroup;
}

/**
 * 
 * @param MEntityGroup
 *            The M_EntityGroup
 */
public void setMEntityGroup(String MEntityGroup) {
    this.MEntityGroup = MEntityGroup;
}

public ReceivedEntityModel withMEntityGroup(String MEntityGroup) {
    this.MEntityGroup = MEntityGroup;
    return this;
}

/**
 * 
 * @return The MEntityTableModule
 */
public List<String> getMEntityTableModule() {
    return MEntityTableModule;
}

/**
 * 
 * @param MEntityTableModule
 *            The M_EntityTableModule
 */
public void setMEntityTableModule(List<String> MEntityTableModule) {
    this.MEntityTableModule = MEntityTableModule;
}

public ReceivedEntityModel withMEntityTableModule(
        List<String> MEntityTableModule) {
    this.MEntityTableModule = MEntityTableModule;
    return this;
}

public Map<String, String> getAdditionalProperties() {
    return this.additionalProperties;
}

public void setAdditionalProperty(String name, String value) {
    this.additionalProperties.put(name, value);
}

public ReceivedEntityModel withAdditionalProperty(String name, String value) {
    this.additionalProperties.put(name, value);
    return this;
}

}

1 个答案:

答案 0 :(得分:0)

列名称在哪里定义?

未使用目标名称创建DefaultDimension2列 在你的桌子上,因为你在这里忘了一个空格:

+ DefaultDimension2 + "INTEGER, "

一定是

 + DefaultDimension2 + " INTEGER, "

因此,假设DefaultDimension2列名称定义为

public String DefaultDimension2 = "DefaultDimension2";

它实际上是"DefaultDimension2INTEGER, "而不是"DefaultDimension2 INTEGER, "

这真是一个常见的错误,主要是因为匆忙。