3

ActiveAndroid:

I need to push updates to specific table fields without wiping out any existing data in the row if i dont happen to save() data for all fields each time.

For example:

I have a table named 'user' which contains the following fields:

extSrc | extId | email | firstName | lastName | role | photo | userId | verified

If i save data to all fields except 'verified' and then I later need to update only the verified field - is this possible?

Currently when i use the code below, all fields except userId (which i have stored locally) and verified are populated, all other fields are cleared. At the time that i have the verified value that i need to update i dont have the additional user data to push / save() so i only need to update the specific 'verified' field when the userId matches a userId in the user table and leave all other fields for that user as they are.

My code:

Model:

    // define table name
    @Table(name = "User")

    public class User extends Model 
    {
    // define table columns

    @Column(name = "extSrc")
    public String extSrc;

    @Column(name = "extId")
    public String extId;

    @Column(name = "email")
    public String email;

    @Column(name = "firstName")
    public String firstName;

    @Column(name = "lastName")
    public String lastName;

    @Column(name = "role")
    public String role;

    @Column(name = "photo")
    public String photo;

    @Column(name = "userId", unique = true, onUniqueConflict = Column.ConflictAction.REPLACE)
    public String userId;

    @Column(name = "verified")
    public String verified;
    }


controller:


    public class UserAdapter implements JsonDeserializer {

    @Override
    public User deserialize(JsonElement arg0, Type arg1,
        JsonDeserializationContext arg2) throws JsonParseException {

            User u = new User();

            Log.v("user", u.toString());

            JsonObject j = (JsonObject) arg0;

            Log.v("j", j.toString());


            if(j.has("extSrc"))
            {
                u.extSrc = j.get("extSrc").getAsString();
                Log.v("extSrc", u.extSrc);
            }
            else
            {
                Log.v("extSrc", "does not exist");
            }


            if(j.has("extId"))
            {
                u.extId = j.get("extId").getAsString();
                Log.v("extId", u.extId);
            }
            else
            {
                Log.v("extId", "does not exist");
            }


            if(j.has("email"))
            {
                u.email = j.get("email").getAsString();
                Log.v("email", u.email);
            }
            else
            {
                Log.v("email", "does not exist");
            }


            if(j.has("firstName"))
            {
                u.firstName = j.get("firstName").getAsString();
                Log.v("firstName", u.firstName);
            }
            else
            {
                Log.v("firstName", "does not exist");
            }


            if(j.has("lastName"))
            {
                u.lastName = j.get("lastName").getAsString();
                Log.v("lastName", u.lastName);
            }
            else
            {
                Log.v("lastName", "does not exist");
            }


            if(j.has("role"))
            {
                u.role = j.get("role").getAsString();
                Log.v("role", u.role);
            }
            else
            {
                Log.v("role", "does not exist");
            }


            if(j.has("photo"))
            {
                u.photo = j.get("photo").getAsString();
                Log.v("photo", u.photo);
            }
            else
            {
                Log.v("photo", "does not exist");
            }


            if(j.has("userId"))
            {
                u.userId = j.get("userId").getAsString();
                Log.v("userId", u.userId);
            }
            else
            {
                Log.v("userId", "does not exist");
            }


            if(j.has("verified"))
            {
                u.userId = SpontlyApplication.PREFS_USER_ID;
                u.verified = j.get("verified").getAsString();

                Log.v("verified", u.verified);
            }
            else
            {
                u.verified = "true";
                Log.v("verified", "does not exist");
            }

     u.save();

     }   

Thanks.

4

1 回答 1

2

如果要更新数据库中已经存在的用户,则需要先从数据库中检索它并在调用 save() 之前更新其字段;

就像是:

User user = new Select().from(User.class).where("userId = ?", userIdYouWantToRetrieve).executeSingle();

if (user != null){
   user.setVerified(true);
} else {
   user = new User(){//populate your new user here from json }
}

user.save();

这将使用户值与以前存储在数据库中的值相同,并且您只会更新所需的新值。

于 2014-01-14T10:17:40.390 回答