0

to update a row in data base when i press a button

public void updateContact(long id, String x, String y,String angle)
        {
        ContentValues editCon = new ContentValues();
        editCon.put(KEY_X, x);
        editCon.put(KEY_Y, y);
        editCon.put(KEY_ANGLE, angle);

        ourDatabase.update(DATABASE_TABLE, editCon, KEY_ROWID + id, null);

        }

and i use it entry.updateContact(1, Double.toString(db1), Double.toString(db2), Double.toString(db3));

but i get the exception: no such colum: _id1 (code1): , while compiling: UPDATE SavedTable SET position_y=?, position_x=?, position_angle=? WHERE_id1

even though i have a database with one row ( i create the entry in my oncreate method of main activity)

2
  • post your full code ,with log Commented Apr 1, 2013 at 12:57
  • Have you updated the database version number since changing any column names? Commented Apr 1, 2013 at 12:58

2 Answers 2

1

KEY_ROWID + id will not work as you expect! KEY_ROWID is probably a String containing _id and your id variable contains 1 which will result in _id1.

You need this:

ourDatabase.update(DATABASE_TABLE, editCon, KEY_ROWID + "=" + id, null);

or better:

ourDatabase.update(DATABASE_TABLE, editCon, KEY_ROWID + "=?", new String[]{String.valueOf(id)});

EDIT:

// pseudocode
Cursor c = ourDatabase.query(DATABASE_TABLE, null, null....);
if (c != null) {
    if (c.getCount() >= 1) {
        // there is an entry - just update
    } else { 
        // no entry - create entry
    }
} else {
    // error? But definitively no entry so create one
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for answer. The thing is I am updating the first row all the time. and each time my app runs I will call createEntry (which i put in onCreate of main activity) which adds one line. Actually I put that createEntry with fake numbers to just have something to update with needed data (when user presses the button). How can I limit all these to only one line of database?
If you just want to have one line in that table, create a createOrUpdate() method. There you need to query the table to see if there is an entry already there. In that case you use an update on that line (the ID should always be 1 in this case). If there is no line in that table, you create one. Simple case handling :)
"There you need to query the table to see if there is an entry already there" Could you plz tell how to do this?
I added some pseudocode of the basic createOrUpdate() method. I think googling create or update database should give some good results, too.
I tried to implement it but have problem I posted it in new thread here could you plz have a look
1

You need to fix your update() method:

db.update(DATABASE_TABLE, editCon, KEY_ROWID + " = ?", 
          new String[] {String.valueOf(id)});

Problem was that you specified incorrect WHERE clause. You wrote this:

KEY_ROWID + id

this is translated on the database layer as _id1 where it was interpreted as column.

You needed _id = 1 and this is achieved by KEY_ROWID + " = ?" where placeholder will be replaced with value from string array.

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.