1

I'm writing a method to update default settings in a table. The table is very simple: two columns, the first containing labels to indicate the type of setting, the second to store the value of the setting.

At this point in the execution, the table is empty. I'm just setting up the initial value. So, I expect that this cursor will come back empty. But instead, I'm getting an error (shown below). The setting that I am working with is called "lastPlayer" and is supposed to get stored in the "SETTING_COLUMN" in the "SETTINGS_TABLE". Here's the code:

public static void updateSetting(String setting, String newVal) { String table = "SETTINGS_TABLE"; String[] resultColumn = new String[] {VALUE_COLUMN}; String where = SETTING_COLUMN + "=" + setting; System.err.println(where); SQLiteDatabase db = godSimDBOpenHelper.getWritableDatabase(); Cursor cursor = db.query(table, resultColumn, where, null, null, null, null); System.err.println("cursor returned"); //I never see this ouput \\more } 

sqlite returned: error code = 1, msg = no such column: lastPlayer

Why is it saying that there is no such column lastPlayer? I thought that I was telling the query to look at the column "SETTING_COLUMN" and return the record where that column has a value "lastPlayer". I'm confused. Can somebody straighten me out? I've been looking a this for an hour and I just don't see what I am doing wrong.

Thanks!

2
  • 1
    Using SharedPreferences is better for your purpose (And much less complicated to use). Commented Dec 6, 2012 at 19:05
  • Good suggestion. I'll look into it and possibly circle back to this. Commented Dec 6, 2012 at 19:13

1 Answer 1

2

You're not properly building/escaping your query. Since the value lastPlayer is not in quotes, your statement is checking for equality of two columns, which is what that error message is saying.

To properly build your query, it's best to not do this manually with String concatenation. Instead, the parameter selectionArgs of SQLiteDatabase.query() is meant to do this.

The parameters in your query should be defined as ? and then filled in based on the selectionArgs. From the docs:

You may include ?s in selection, which will be replaced by the values from selectionArgs, in order that they appear in the selection. The values will be bound as Strings.

So, your code would look like this:

String where = SETTING_COLUMN + " = ?"; Cursor cursor = db.query(table, resultColumn, where, new String[] { setting }, null, null, null); 
Sign up to request clarification or add additional context in comments.

1 Comment

That was it. Learned something new about SQLite. Thank you!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.