tags:

views:

139

answers:

2

Why doesn't this work??

db.execSQL("INSERT INTO PARTIES (PARTY_NAME, PARTY_COUNT) SELECT DISTINCT(PARTY), COUNT(PARTY) FROM ? WHERE (Year=?) GROUP BY PARTY ORDER BY PARTY ASC", new Object[] { "Election", "2004" });

It works perfectly in rawQuery!!

+1  A: 

I don't know what "rawQuery" you're referring to (URL please). I don't know of any SQL engine which allows parameter substitution for metadata such as names of tables and columns -- only values, nor names, are allowed in parameter substitution. SQLite is no exception.

Alex Martelli
He's talking about the `rawQuery()` method on the `SQLiteDatabase` object: http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#rawQuery%28java.lang.String,%20java.lang.String[]%29
fiXedd
@fiXedd, tx -- the discussion on the other "answer" makes more sense now;-).
Alex Martelli
A: 

Thanks you're right. The rawQuery function doesn't error when binding a table name, but it doesn't work anyway. ExecSQL errors on binding a table name.

Kurian
This should have been posted as a comment on the above answer.
Christopher
Sorry I had no idea. Just joined today.
Kurian
Actually, rawQuery *does* throw an exception when compiling an invalid binding like the one mentioned:02-17 17:42:04.880: ERROR/AndroidRuntime(344): Caused by: android.database.sqlite.SQLiteException: near "?": syntax error: , while compiling: select * from ?
dtmilano