lundi 30 mars 2015

database field names and field types combining at fieldname

currently i'm developing erp project and there is a json data to write database,i convert json to java before, but when i try to put inside database there is such no column exception,because column names doesnt match and invalid.


here is my code.



@SuppressLint("ShowToast") public class DatabaseEntityOperations {

private String[] entityLabel = null;
Database dbHelper;

public DatabaseEntityOperations(Database sDb) {
dbHelper = sDb;
}

private static final String TABLE_2_NAME = "entity";

public String rId = "rId";
private static String ParentId = "ParentId";
public String Code = "Code";
public String Label = "Label";
private static String LabelFL = "LabelFL";
private static String EntityGroupId = "EntityGroupId";
private static String Property = "Property";
private static String ReportCode = "ReportCode";
private static String Description = "Description";
private static String Code1Id = "Code1Id";
private static String Code2Id = "Code2Id";
private static String Code3Id = "Code3Id";
private static String Code4Id = "Code4Id";
private static String Code5Id = "Code5Id";
private static String DefaultDimension1 = "DefaultDimension1";
private static String DefaultDimension2 = "DefaultDimension2";
private static String DefaultDimension3 = "DefaultDimension3";
private static String CompanyId = "CompanyId";
private static String BranchId = "BranchId";
private static String ReferenceDate = "ReferenceDate";
private static String ResField1 = "ResField1";
private static String ResField2 = "ResField2";
private static String AreaId = "AreaId";
private static String ModifiedDateTime = "ModifiedDateTime";
private static String CreatedDateTime = "CreatedDateTime";
private static String RecId = "RecId";
private static String IsActive = "IsActive";
private static String ModifiedBy = "ModifiedBy";
private static String CreatedBy = "CreatedBy";

public void onCreate(SQLiteDatabase db) {


//String sql2="CREATE TABLE ["+TABLE_2_NAME+"] ([rId] integer, [ParentId] integer DEFAULT 0,[Code] nvarchar(50) NOT NULL COLLATE NOCASE,[Label] nvarchar(100) COLLATE NOCASE,[LabelFL] nvarchar(100) COLLATE NOCASE,[EntityGroupId] integer,[Property] nvarchar(100) COLLATE NOCASE,[ReportCode] nvarchar(100) COLLATE NOCASE,[Description] nvarchar(100) COLLATE NOCASE,[Code1Id] integer,[Code2Id] integer,[Code3Id] integer,[Code4Id] integer,[Code5Id] integer,[DefaultDimension1Id] integer,[DefaultDimension2Id] integer,[DefaultDimension3Id] integer,[CompanyId] integer NOT NULL,[BranchId] integer NOT NULL,[ReferenceDate] datetime,[ResField1] nvarchar(50) COLLATE NOCASE,[ResField2] nvarchar(50) COLLATE NOCASE,[AreaId] nvarchar(4) NOT NULL COLLATE NOCASE DEFAULT 'dnt',[ModifiedDateTime] datetime,[CreatedDateTime] datetime,[RecId] integer PRIMARY KEY AUTOINCREMENT NOT NULL,[IsActive] bit NOT NULL DEFAULT 1,[ModifiedBy] integer,[CreatedBy] integer);";

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

//db.execSQL(sql2);
}

public Cursor fetchEntities()
{
SQLiteDatabase db = dbHelper.getWritableDatabase();
String myQuery="SELECT * 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;
}

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().toString());
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();

}


}


and here is the result of the sqliteCursor's(mColumns) column names at debug screen.As you see there is matching and mismatching situation between field names.



[rId, ParentIdINTEGER, CodeINTEGER, LabelTEXT, LabelFLTEXT, EntityGroupIdINTEGER, PropertyTEXT, ReportCodeINTEGER, DescriptionTEXT, Code1IdINTEGER, Code2IdINTEGER, Code3IdINTEGER, Code4IdINTEGER, Code5IdINTEGER, DefaultDimension1INTEGER, DefaultDimension2INTEGER, DefaultDimension3INTEGER, CompanyId, BranchId, ReferenceDate, ResField1, ResField2, AreaId, ModifiedDateTime, CreatedDateTime, RecId, IsActive, ModifiedBy, CreatedBy]

Aucun commentaire:

Enregistrer un commentaire