1

我正在使用 Retrofit 来访问数据和存储我正在使用 SugarRecord。我是 SugarRecord Orm 的新手。

显现:

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"

Cls文档:

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

我没有发布的 Setter 和 getter 这是 ArrayFormat ClsDocumentList 中的 ClsDocument 列表

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

在 onCreate 中初始化数据库:

SugarContext.init(getApplicationContext());

这是我的演示代码:

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

        }
    });
}

这是一个日志:

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)

感谢您的指导。

4

1 回答 1

2

更改您的模型类文件ClsDocumentList,如下所示。

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

    @SerializedName("DOCUMENT_TYPE")
    private String dOCUMENTTYPE;
}

替换您的SugarRecordto SugarRecord<ClsDocumentList>which 会将此类映射到SugarRecord Table.

有关更多信息,请参阅此链接以使用 SugarRecord 创建表。

于 2018-03-19T09:35:18.913 回答