Graeme
Graeme

Reputation: 427

Android sqlite rawquery selection args problems

I'm trying to run use rawquery to run a query with an alias on the primary key to make it _id so that I can use the SimpleCursorAdapter, this is what I have:

String sel = DatabaseListDB.COLUMN_DATABASE_NAME;
Cursor results = db.rawQuery("SELECT ? as _id FROM DatabaseList", 
    new String[] {sel});

In my SqlLiteHelperClass

public static final String COLUMN_DATABASE_NAME = "databaseName";

is the column name I want and my primary key. The problem is when I run this query the results come up with 'databaseName' as the values in the table instead of the actual values. I can run the query as

Cursor results = db.rawQuery("SELECT databaseName as _id FROM DatabaseList", 
    null);

and it works fine and I have no idea why I cant use the selectionargs properly. Can anyone help me fix this?

Upvotes: 1

Views: 2213

Answers (2)

waqaslam
waqaslam

Reputation: 68177

? is supposed to use for WHERE arguments. In your case, you should simply alter your query as:

Cursor results = db.rawQuery(String.format("SELECT %s as _id FROM DatabaseList", sel));

Upvotes: 2

secretlm
secretlm

Reputation: 2361

From developer.android.com, you only can put "?" on where clause, not for columns you want to query:

  • selectionArgs: You may include ?s in where clause in the query, which will be replaced by the values from selectionArgs. The values will be bound as Strings.

Upvotes: 1

Related Questions