0
SELECT * "
+ "FROM Table1 "
+ "WHERE Username = ? "
+ "AND Password = ?;");

PreparedStatement.setString(1, "Albert");
PreparedStatement.setString(2, "123"); OR USING VARIABLES

THIS WORKS

SELECT * "
+ "FROM ? "
+ "WHERE Username = ? "
+ "AND Password = ?;");

myPreStatement.setString(1, "Table1");
myPreStatement.setString(2, "Albert");
myPreStatement.setString(3, "123"); OR USING VARIABLES

THIS DOES NOT :

java.sql.SQLException: [SQLITE_ERROR] SQL error or missing database (near "?": syntax error)

Why can I not use a ? in the FROM section. Is this not allowed or is my statement wrong in someway?

I need to use a ? in the FROM section as I have a function using this statement for multiple tables. I could just copy this function 3 times. One for each table but that seems pointless.

Dynermite
  • 189
  • 2
  • 13
  • 2
    The `?` placeholder doesn't work for table names or column names, it only works for values . – Arnaud Mar 14 '17 at 15:05
  • To add to the comment by @Berger, typically the table being used in a query would be fixed on the server side, and not controllable by the client anyway. Hence, prepared statements don't support table names in this way. – Tim Biegeleisen Mar 14 '17 at 15:06

0 Answers0