Android: what is wrong with my create table statement?



My onCreate method of the SQLiteOpenHelper contains the following code:

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");

The Logcat output contains only the first Log.d:

oncreate is called

And then the app crashes. What is wrong with my create table statement?


Logcat actually says that the table TrainingSession already exists. However, my onUpgrade method looks like this:

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

and the Logcat output of that method is as expected, the method does get called:

onupgrade is called

If onUpgrade is called and the tables are deleted, then why does onCreate say that TrainingSession already exists?



added in API level 1
public void execSQL (String sql)
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.
statements separated by semicolons are not supported.

So execSql() does not support multiple statements separated with ;
So execute multiple execSql() each for every statement:

public static final String SQL_DELETE_TABLE1 = "DROP TABLE IF EXISTS TrainingSet"
public static final String SQL_DELETE_TABLE2 = "DROP TABLE IF EXISTS TrainingSession"

Answered By – forpas

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More