0

I've look into other questions regarding the same general idea (e.g. Python MySQL - SELECTs work but not DELETEs?), but no luck.

I have the following code:

cursor.execute("DELETE FROM ? WHERE source_id = ?", (tableName,sourceId) )

It fails with:

sqlite3.OperationalError: near "?": syntax error

However, this way it works:

cursor.execute("DELETE FROM %s WHERE source_id = %s" % (tableName,sourceId) )

The same query was tested directly in SQL terminal, so this is not a question of user rights or foreign keys.

I'm still rather fresh with some python sql issues, so thanks in advance.

Community
  • 1
  • 1
Michał Leon
  • 2,108
  • 1
  • 15
  • 15
  • 4
    placeholders can only be stand-ins for SQL Values, not arbitrary sql expressions. what's the precise problem you're trying to solve. – SingleNegationElimination Dec 19 '14 at 22:31
  • Thanks! You mean no parameter for table name? – Michał Leon Dec 19 '14 at 23:05
  • 1
    Right -- table names, field names, &c, can't be represented by '?' placeholders -- only "literal constants" can. All versions of SQL I know worn that way. – Alex Martelli Dec 19 '14 at 23:19
  • The reason for this is that if table names (part of the structure/code), where passed in as parameters (which is data), the database engine does not know what the final query looks like. This would mean that things such as optimization (via the planner) of the query could not be done correctly (if at all). – Timusan Dec 20 '14 at 01:50
  • possible duplicate of [What Parts of a SQLite Statement Can Have Bound Parameters?](http://stackoverflow.com/questions/15513675/what-parts-of-a-sqlite-statement-can-have-bound-parameters) – CL. Dec 20 '14 at 08:16

0 Answers0