Android:我的 create table 语句有什么问题?

Android: what is wrong with my create table statement?

我的 SQLiteOpenHelper 的 onCreate 方法包含以下代码:

Log.d("mytag", "oncreate is called");
db.execSQL("CREATE TABLE TrainingSession (id INTEGER PRIMARY KEY, session_date TEXT, session_status TEXT)");
Log.d("mytag", "tables were created");

Logcat 输出仅包含第一个 Log.d:

oncreate is called

然后应用程序崩溃了。我的 create table 语句有什么问题?

编辑:

Logcat 实际上是说 table TrainingSession 已经存在。但是,我的 onUpgrade 方法如下所示:

Log.d("mytag", "onupgrade is called");
db.execSQL("DROP TABLE IF EXISTS TrainingSet; DROP TABLE IF EXISTS TrainingSession;");
onCreate(db);

并且该方法的 Logcat 输出符合预期,该方法确实被调用:

onupgrade is called

如果调用了onUpgrade,删除了table,那为什么onCreate说TrainingSession已经存在了?

如果查询没问题,如果数据库结构发生变化,这些事情就会经常发生。

尝试:

选项 1 : 添加其他 sqlite 数据库版本。

选项 2 : 删除数据库并重新创建它。

选项 3:从 phone 应用信息屏幕清除应用数据。

来自https://developer.android.com/reference/android/database/sqlite/SQLiteDatabase#execSQL(java.lang.String)

execSQL
added in API level 1
public void execSQL (String sql)
Execute a single SQL statement that is NOT a SELECT or any other SQL statement that returns data.
..............................
Parameters sql String:
the SQL statement to be executed.
Multiple statements separated by semicolons are not supported.

因此execSql()不支持用分隔的多语句;
所以对每个语句分别执行多个execSql()

public static final String SQL_DELETE_TABLE1 = "DROP TABLE IF EXISTS TrainingSet"
db.execSQL(SQL_DELETE_TABLE1); 
public static final String SQL_DELETE_TABLE2 = "DROP TABLE IF EXISTS TrainingSession"
db.execSQL(SQL_DELETE_TABLE2);