You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Retrofit+SugarRecord时遇No table found问题求助

解决SugarRecord报错"No table found"的问题

Hey there, let's work through this "No table found" error you're hitting with SugarRecord and Retrofit. I’ve been in your shoes when starting out with SugarRecord—small oversights can cause this, so let’s break down the fixes step by step:

1. 你的模型类必须继承SugarRecord

This is the most common culprit! SugarRecord won’t generate a database table for your ClsDocument class unless it extends SugarRecord. Update your class like this:

public class ClsDocument extends SugarRecord<ClsDocument> {
    @SerializedName("SUCCESS")
    @Expose 
    private String sUCCESS;
    // 其他你的字段...

    // 重要:必须添加无参构造函数!SugarRecord依赖它来实例化对象
    public ClsDocument() {
        super();
    }

    // 如果你需要带参数的构造函数,可以在这里添加
    // public ClsDocument(String success) {
    //     this.sUCCESS = success;
    // }
}

(Note: Depending on your SugarRecord version, you might also be able to just extend SugarRecord without the generic type.)

2. 验证Manifest配置的正确性

Double-check that your meta-data tags are nested inside the <application> tag (not outside), and that the DOMAIN_PACKAGE_NAME matches exactly where your ClsDocument class lives. Your Manifest should look like this:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    
    <!-- 你的Activity配置 -->
    <activity android:name=".MainActivity">...</activity>

    <!-- SugarRecord配置 -->
    <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" />
</application>

3. 处理数据库版本冲突

You’ve set your database version to 3—if you had an older version of the app installed before, the existing database might be causing issues. For testing purposes, uninstall the app from your device/emulator and reinstall it to clear the old database files. This ensures SugarRecord creates a fresh database with the correct tables.

4. 正确保存Retrofit返回的数据

Make sure when you get the data from Retrofit, you’re calling SugarRecord’s save() method on each ClsDocument instance. For example:

// 假设这是你的Retrofit回调
call.enqueue(new Callback<ClsDocument>() {
    @Override
    public void onResponse(Call<ClsDocument> call, Response<ClsDocument> response) {
        if (response.isSuccessful()) {
            ClsDocument document = response.body();
            // 保存到数据库
            document.save();
        }
    }

    @Override
    public void onFailure(Call<ClsDocument> call, Throwable t) {
        // 处理错误
    }
});

If you’re getting a list of ClsDocument objects, loop through them and save each one individually.

5. 检查ProGuard混淆(如果启用)

If you’re using ProGuard in your project, it might be obfuscating your model class, which prevents SugarRecord from recognizing it. Add these rules to your proguard-rules.pro file:

# 保留你的模型类包下的所有类
-keep class com.example.abhishek.ModelClass.** { *; }
# 保留SugarRecord相关类
-keep class com.orm.** { *; }

6. 启用查询日志排查(可选)

If you’re still stuck, temporarily set QUERY_LOG to true in your Manifest. This will log all SQL statements SugarRecord executes, so you can check if the table creation query is running successfully.

Give these steps a try—most likely, the missing inheritance or incorrect Manifest setup is the issue. Let me know if you run into any other snags!

内容的提问来源于stack exchange,提问作者Abhishek Bardolia

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:13:41