ExecSQL SQLiteException next to "?"

Why is this not working?

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 great in rawQuery!

+3
source share
2 answers

I don't know what kind of "rawQuery" you are referring to (URL, please). I don’t know a single SQL mechanism that allows parameter substitution for metadata, such as table and column names — only values ​​or names are allowed when parameter substitution. SQLite is no exception.

+2
source

Maybe it shouldn't be?

copypaste from doc:

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

Execute one SQL statement, which is NOT. SELECT/INSERT/UPDATE/DELETE. For statements, INSERTuse any of the following:

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

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

+1

Source: https://habr.com/ru/post/1733121/


All Articles