-1

Android Studio 3.1

purpose: update version number of sqlite database

error

expected, got '?'

the code caused error was

db.execSQL("PRAGMA user_version = ?", new Object[]{questionDbVersion});

android.database.sqlite.SQLiteException: near "?": syntax error (code 1): , while compiling: PRAGMA user_version = ? at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)

but in the Sql environment, the sql sentence worked well

PRAGMA user_version =1
PRAGMA user_version
PRAGMA user_version =2

What should I do?

============ I found a solution

db.setVersion(questionDbVersion);
double-beep
  • 5,031
  • 17
  • 33
  • 41
Jack Wilson
  • 6,065
  • 12
  • 29
  • 52

1 Answers1

1

In SQL grammar as understood by sqlite, PRAGMA value cannot be a variable, and ? denotes a variable.

The first error is from Android Studio static analyzer. There have been false positives (such as this) especially with version 3.0.x but this one is valid error.

The second error is a runtime error for the same.

Use a hardcoded string or String.format() formatting for PRAGMAs, or as you've found out, for this particular pragma you can use setVersion().

laalto
  • 150,114
  • 66
  • 286
  • 303