2

I'm creating an activity that views a record and displays it's data to a user to modify. After everything is done just click a button to save the data back into the database. However, once saving time comes around I get an unusual "java.lang.IllegalArgumentException: Empty values" error. I think it's a SQL code error on my end, but I don't know enough about SQL to find it. Can I get a couple of eyes to see what I did wrong?

Thanks ~Aedon

Here is where the save button is declared and the listener is set. mBoundService is the service that hosts the database and it's calls. DevicesTable is a class that holds the table and column names. DevicesTable.gt_fields[0] is autoincrementID and then serial, name, and first seen columns

public void init() {        
    mDevName = (EditText)findViewById(R.id.dv_name);
    mCurReads = (TextView)findViewById(R.id.dv_readings);
    mSave = (Button)findViewById(R.id.dv_save);
        mSave.setOnClickListener(new OnClickListener() {
            @Override public void onClick(View arg0) {
                mBoundService.updateRecord(DevicesTable.gt_name, mId, DevicesTable.g_fields[2], mDevName.getText().toString());
            }
        });
}

And this is the database call itself.

/**
 * Updates a record at the given table with the given record.
 * @param table The table to update
 * @param id The id of the actual record. Do not pass the incorrect id!
 * @param column The column in which the data is changing
 * @param data The data that is to be changed to
 */
public void updateRecord(String table, String id, String column, String data) {
    mDB.update(table, null, "SET " + column + " = '" + data + "',", new String[]{"id=" + id});
}

1 Answer 1

4

You need to pass in a ContentValues object to tell the database what values go in what columns in the updated row(s). The strings in the third and fourth arguments make up a WHERE clause that indicate what row(s) should be updated. For example:

ContentValues values = new ContentValues();
values.put("foo", 123);
values.put("bar", 456);

db.update("some_table", values, "id=789", null);

This would be equivalent to issuing the query

UPDATE some_table SET foo = 123, bar = 456 WHERE id = 789

Your code attempts to stuff the entire query into the WHERE clause, and update() stops you in your tracks because the values argument is null.

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

2 Comments

I see thank you, I was looking at wrong. I thought the documentation was saying that ContentValues was optional and I could do it through string manipulation via the WHERE statement.
You were probably thinking of something along the lines of db.rawQuery("UPDATE some_table SET foo = ?, bar = ? WHERE id = ?", new String[] {"123", "456", "789"}).

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.