1

in my android app I am extending SQLiteOpenHelper to create a SQLite database.

Here is my onCreate method:

@Override
public void onCreate(SQLiteDatabase database) {
    String sql = "CREATE TABLE ? ( " + SuggestionColumns._ID + " INTEGER PRIMARY KEY)"; 
    String[] params = new String[]{SuggestionColumns.TABLE_NAME};
    database.execSQL(sql, params);

But LogCat prompts me the following error:

10-10 00:55:03.255: E/SQLiteLog(22311): (1) near "?": syntax error
10-10 00:59:20.605: W/SuggestionsAdapter(22311): Search suggestions query threw an exception.
10-10 00:59:20.605: W/SuggestionsAdapter(22311): android.database.sqlite.SQLiteException: near "?": syntax error (code 1): , while compiling: CREATE TABLE ? ( _id INTEGER PRIMARY KEY);

But if I substitute the placeholder with a string and call execSql(sql); it works.

So the question is: is it forbidden to use placeholders in CREATE TABLE statements?

1 Answer 1

1

Placeholders are limited to literals, such as the right-hand side of comparison statements in WHERE clauses, or the values to be inserted in an INSERT statement. Placeholders cannot be used for other portions of a SQL statement, such as table or column names.

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

2 Comments

Oh ok, thank you. Is it present in the official documentation? I spent hours searching for it, with no result!
@user2340612: In the SQLite documentation for the C API, yes: sqlite.org/c3ref/bind_blob.html

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.