3

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
  • I've never seen a table name be passed as a parameter before. But maybe I just never tried... Parameters are typically used to allow reuse of the same query plan. So you can just provide a different value filter on each run. In your case, why would the table be dynamic? Commented Feb 17, 2010 at 5:10

2 Answers 2

2

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.

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

2 Comments

He's talking about the rawQuery() method on the SQLiteDatabase object: developer.android.com/reference/android/database/sqlite/…
@fiXedd, tx -- the discussion on the other "answer" makes more sense now;-).
1

May be it should not?

copypaste from doc:

public void execSQL (String sql, Object[] bindArgs)

Execute a single SQL statement that is NOT a SELECT/INSERT/UPDATE/DELETE. For INSERT statements, use any of the following instead.

  • insert(String, String, ContentValues)
  • insertOrThrow(String, String, ContentValues)
  • insertWithOnConflict(String, String, ContentValues,int)

http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html

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.