Home > Software design >  Table is not created
Table is not created

Time:09-15

i womder why i cannot create the table here, android studio never showed a syntax error

class SQLiteHelper (context: Context) :
    SQLiteOpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION){
    
    companion object{

        private const val DATABASE_VERSION = 1
        private const val DATABASE_NAME = "student.db"
        private const val TBL_STUDENT = "tbl_student1"
        private const val ID = "id"
        private const val NAME = "name"
        private const val EMAIL = "email"

    }

    override fun onCreate(db: SQLiteDatabase?) {
        val createTBlStudent = ("CREATE TABLE "   TBL_STUDENT   " ("
                  ID   " INTEGER PRIMARY KEY, "   NAME   " TEXT, "
                  EMAIL   " TEXT "   ")")  
        db?.execSQL(createTBlStudent)   
    }
}

CodePudding user response:

The most likely issue is that you have not accessed the database and thus it will not exist.

Another issue could be that you have run the code, the table create failed and you have not deleted the empty database. If this is the case just uninstall the App and rerun.

  • that is if onCreate has been called, the database has been created and onCreate will not be run again, even if onCreate fails, unless the database is deleted (uninstalling being the simplest way).

If you just instantiate using SQLiteHelper then the database will not be created. You have to try to do something with the database before the onCreate method is called.

The following shows this (this assumes that you have included an override of the onUpgrade method (which need not do anything within the body)). This code uses an exact copy of your code (BUT with onUpgrade overidden).

In an Activity :-

class MainActivity : AppCompatActivity() {
    lateinit var helper: SQLiteHelper
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main) 

        helper= SQLiteHelper(this)
        Log.d("DBSTATUS_BEFORE","Database ${this.getDatabasePath(helper.databaseName)} EXISTS ${File(this.getDatabasePath(helper.databaseName).path).exists()}")
        helper.writableDatabase //* force access the database i.e force an Open*//
        Log.d("DBSTATUS_AFTER","Database ${this.getDatabasePath(helper.databaseName)} EXISTS ${File(this.getDatabasePath(helper.databaseName).path).exists()}")
    }
}

When run as a fresh install (i.e. when no database exists) then the Log includes:-

2022-09-14 15:03:14.588 D/DBSTATUS_BEFORE: Database /data/user/0/a.a.so73657002kotlinsqlite/databases/student.db EXISTS false
2022-09-14 15:03:14.634 D/DBSTATUS_AFTER: Database /data/user/0/a.a.so73657002kotlinsqlite/databases/student.db EXISTS true

As can be seen it is not until the database is accessed and a writable (or readable which will very very likely be a writable) database is obtained, that the database is in fact created.

Of course unless the App in uninstalled the database then exists, so for the lifetime of the database, that is the only time that the onCreate method is called automatically.

The using App inspection you can see:-

enter image description here

  • No data exists in the database, but the database exists.
  • Ignore the other database, it exists because I used existing code to provide this answer.

Of course as you can see your code is good and works (other than what has been said about the onUpgrade method)

  • Related