0

I'm sure there are lots of similar question but my context is kinda different.

So i'm working on an android app and i'm inserting data into a table. Let's say the table name is user with columns firstname and lastname which are of type text.

I run an insert function to insert a row into the table but with information for just firstname. So firstname is inserted and lastname is empty. Now here come the problem.

When I try to update the column lastname, it doesn't work. I don't know if it's cos it empty or null.

My update code looks something like

 int result = db.update(TABLE_USER, contentvalue, "_id= someValue", null); 

Result is usually 0. Contentvalue contains lastname and I can guarantee it's not empty.

I don't know if during the first insert, it's mandatory to set the value of the last to null or empty and not let the db handle that cos it works if I do. Any suggestion is appreciated and i'ld like to know why it doesn't work too. Thanks

1
  • does your table have the column _id ? did you debug/log the value someValue? is it correct? did you make sure that the row is actually inserted? otherwise you will be updating a row that does not exist Commented Nov 7, 2016 at 14:28

2 Answers 2

1

No during insert, it is not mandatory to fill all columns. If you want to update a value, then you can take reference from my method. After creating this method only you have to call this method by object of dbhelper class.

public void updateData(String id, String eventname, String strdate, String strtime, String enddate, String endtime) 
{
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COLUMN_EVENTNAME,eventname);
    contentValues.put(COLUMN_STARTDATE,strdate);
    contentValues.put(COLUMN_STARTTIME,strtime);
    contentValues.put(COLUMN_ENDDATE, enddate);
    contentValues.put(COLUMN_ENDTIME,endtime);

    db.update(TABLE_NAME, contentValues, COLUMN_ID+"="+id, null);
}

Make accepted if this will work....thanks in advance

Sign up to request clarification or add additional context in comments.

4 Comments

But you see, this is technically what i've been doing with no results
you are updating the column value in table so how can you store result.if you want to see updated result then again you have to write query like SELECT * FROM TABLE_NAME then you'll see updated result.
int result returns 1 if true and 0 if false. Thanks how u know if an update went through or not.
hey make accepted if you got reference from this. @MueyiwaMosesIkomi
0

Got it to work with

int result = database.update(TABLE_USERS, contentValues, "firstname = ?" , new String[] {firstname});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.