0

I move database create table from Applcation.onCreate to SqliteHelper.onCreate.

In Application, I must check if table already exists. But in SqliteHelper, as it's called on creation of database file, I don't think it's nessasary to use "IF NOT EXISTS"

CREATE TABLE {TABLE_NAME} IF NOT EXISTS (_id bla bla);

Can I change this sql to below?

CREATE TABLE {TABLE_NAME} (_id bla bla)

EDIT ---------

I asked if SqliteHelper.onCreate can called several times.

ceram1
  • 515
  • 1
  • 6
  • 19

2 Answers2

3

onCreate() will only be called if the database does not exist. Adding IF NOT EXISTS is therefore not necessary and would only confuse people reading and maintaining the code.

Also, the syntax would be CREATE TABLE IF NOT EXISTS table_name.

laalto
  • 150,114
  • 66
  • 286
  • 303
2

Yes you can. But the first method is always encouraged!

AabidMulani
  • 2,325
  • 1
  • 28
  • 47