错误插入:在Room数据库中失败的NOT NULL约束(错误代码1299)。

huangapple go评论73阅读模式
英文:

Error Inserting : NOT NULL constraint failed: (code 1299) in Room Database

问题

我正在尝试向我的表中插入值,但每次都会给我以下错误:android.database.sqlite.SQLiteConstraintException: NOT NULL constraint failed: UserModel.Id (code 1299)。很奇怪的是,相同的代码在调试构建中是可以工作的,但当它是一个签名的 APK 时,就会出现这样的错误。

我的 UserModel 如下所示:

@Entity
public class UserModel {
    @PrimaryKey
    @NonNull
    String Id;
    String Fname, Sname;

    @NonNull
    public String getId() {
        return Id;
    }

    public void setId(@NonNull String id) {
        Id = id;
    }

    public String getFname() {
        return Fname;
    }

    public void setFname(String fname) {
        Fname = fname;
    }

    public String getSname() {
        return Sname;
    }

    public void setSname(String sname) {
        Sname = sname;
    }
}

在将数据插入表中时,我使用了 Gson,代码如下所示:

JSONArray payloadArray = new JSONArray(response);
Type type = new TypeToken<List<UserModel>>() {}.getType();
List<UserModel> userList = new Gson().fromJson(payloadArray.toString(), type);
database.userDao().insertData(userList);

在我的构建变体中,我进行了以下设置:

storeFile file('E:\\KeystoreFile\\Sample.jks')
------------------------------------------------------------------
debug {
    minifyEnabled false
    debuggable true
    initWith debug
    proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    buildConfigField "String", "SERVER_URL", project.properties["server.url"]
    proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'

    signingConfig signingConfigs.config
    manifestPlaceholders = [
            appIcon: "@mipmap/ic_launcher"
    ]

    applicationIdSuffix ".qa"
}

我的 Room 版本是 2.0.0。我已经尝试过最新版本 2.2.3 和其他版本,似乎没有任何作用。此外,我已经迁移到 AndroidX,不确定是什么原因导致了这个错误,任何帮助将会很棒。

谢谢

英文:

I am trying to insert values into my table but every time it gives me the following error , android.database.sqlite.SQLiteConstraintException: NOT NULL constraint failed: UserModel.Id (code 1299) . It's very strange that the same code works in debug build but when it's a signed apk it gives me such error.

My UserModel Looks like this:

@Entity
public class UserModel {
@PrimaryKey
@NonNull
String Id;
String Fname,Sname;

@NonNull
public String getId() {
    return Id;
}

public void setId(@NonNull String id) {
    Id = id;
}

public String getFname() {
    return Fname;
}

public void setFname(String fname) {
    Fname= fname;
}

public String getSname() {
    return Sname;
}

public void setSname(String sname) {
    Sname= sname;
}
}

While inserting the data in to table I've used Gson where it looks like:

     JSONArray payloadArray = new JSONArray(response);
     Type type = new TypeToken&lt;List&lt;UserModel&gt;&gt;() {}.getType();
     List&lt;UserModel&gt; userList = new Gson().fromJson(payloadArray.toString(), type);
     database.userDao().insertData(userList);

In my build variant I've set the following:

   storeFile file(&#39;E:\\KeystoreFile\\Sample.jks&#39;)
   ------------------------------------------------------------------
   debug {
        minifyEnabled false
        debuggable true
        initWith debug
        proguardFiles getDefaultProguardFile(&#39;proguard-android.txt&#39;), &#39;proguard-rules.pro&#39;
        buildConfigField &quot;String&quot;, &quot;SERVER_URL&quot;, project.properties[&quot;server.url&quot;]
        proguardFiles getDefaultProguardFile(&#39;proguard-android-optimize.txt&#39;), &#39;proguard-rules.pro&#39;

        signingConfig signingConfigs.config
        manifestPlaceholders = [
                appIcon: &quot;@mipmap/ic_launcher&quot;
        ]

        applicationIdSuffix &quot;.qa&quot;

    }

and my room version is 2.0.0. I tired with latest version 2.2.3 and other versions as well doesn't seem to do anything .Also I've migrated to Androidx not sure what causing this error, any help would be great

Thank you

答案1

得分: 2

你可以查看以下链接获取更多信息 ProGuard规则

至于我的问题,我已经设置了ProGuard规则如下:

-keep class  app.mypackage.model.** { *; }
英文:

For more info you can check the following link ProGuard Rules

And for my problem I've set proGuard rule as :

-keep class  app.mypackage.model.** { *; }

答案2

得分: 0

当您创建签名 APK 时,您的 UserModel 类变量发生了变化,这会在插入时引发问题。
请添加以下代码到 proguard 规则 中:

-keep class * extends androidx.room.RoomDatabase
-dontwarn androidx.room.paging.**
-keepattributes Signature, InnerClasses, EnclosingMethod
-keepattributes RuntimeVisibleAnnotations, RuntimeVisibleParameterAnnotations
英文:

when you create sign apk, your UserModel class variable changed. and its create issue when insert.
add below code for proguard rules

-keep class * extends androidx.room.RoomDatabase
-dontwarn androidx.room.paging.**
-keepattributes Signature, InnerClasses, EnclosingMethod
-keepattributes RuntimeVisibleAnnotations, RuntimeVisibleParameterAnnotations

huangapple
  • 本文由 发表于 2020年1月6日 21:05:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/59612657.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定