androidsqlitesugarormsugarrecord

No table found in SugarRecord using Retrofit


I am using Retrofit to access data and for storing I am using SugarRecord.I am new to SugarRecord Orm.

Manifest:

meta-data
        android:name="DATABASE"
        android:value="sugar_example.db"
meta-data
        android:name="VERSION"
        android:value="3"
meta-data
        android:name="QUERY_LOG"
        android:value="false"
meta-data
        android:name="DOMAIN_PACKAGE_NAME"
        android:value="com.example.abhishek.ModelClass"

ClsDocument:

public class ClsDocument {
@SerializedName("SUCCESS")
@Expose
private String sUCCESS;
@SerializedName("DATA")
@Expose
private List<ClsDocumentList> dATA = null;
}

Setter and getter I had not posted Here is ClsDocument List which is in ArrayFormat ClsDocumentList:

public class ClsDocumentList extends SugarRecord {
@SerializedName("SCHEME_ID")
private String sCHEMEID;
@SerializedName("DOCUMENT_TYPE")
private String dOCUMENTTYPE;
}

Initialise DataBase in onCreate:

SugarContext.init(getApplicationContext());

Here is my DemoCode:

private void demoRetrofit() {
    InterfaceDocument interfaceDocument = ApiClient.getClient().create(InterfaceDocument.class);
    Call<ClsDocument> call = interfaceDocument.CLS_DOCUMENT_CALL();

    call.enqueue(new Callback<ClsDocument>() {
        @Override
        public void onResponse(Call<ClsDocument> call, Response<ClsDocument> response) {
            Log.e(TAG, "onResponse: "+response.body().getSUCCESS() );

            String success=response.body().getSUCCESS();
            if(success.equalsIgnoreCase("1")){
                for (ClsDocumentList objList:response.body().getDATA()) {
                    objList.save();//To save in SugarRecord
                }
            }
        }

        @Override
        public void onFailure(Call<ClsDocument> call, Throwable t) {

        }
    });
}

Here is a Log:

android.database.sqlite.SQLiteException: no such table: CLS_DOCUMENT_LIST (code 1): , while compiling: INSERT OR REPLACE  INTO CLS_DOCUMENT_LIST(ID,S_CHEMEID,D_OCUMENTNAME,D_OCUMENTTYPE,S_ELECTTYPE,D_OCUMENTMODULE,M_ANDATORY) VALUES (?,?,?,?,?,?,?)
                                                                              at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)

Grateful for your guidence.


Solution

  • Change your model class file ClsDocumentList as coded below.

    public class ClsDocumentList extends SugarRecord<ClsDocumentList> {
        @SerializedName("SCHEME_ID")
        private String sCHEMEID;
    
        @SerializedName("DOCUMENT_TYPE")
        private String dOCUMENTTYPE;
    }
    

    Replace your SugarRecord to SugarRecord<ClsDocumentList> which will map this class to SugarRecord Table.

    For more, refer this link for creating Table using SugarRecord.