title: android.database.sqlite.SQLiteException: no such table: countries (code 1 SQLITE_ERROR)
body: <p>I just implemented SQLite database in my project, the app worked perfectly in the old app but after I transfer database files to another app and try to run it, It gave me this error</p>
  <pre><code>android.database.sqlite.SQLiteException: no such table: countries (code 1 SQLITE_ERROR): , while compiling: SELECT * FROM countries
  </code></pre>
  <p>Now I tried to run the old app that was working fine, now that is not working too and giving the same error.</p>
  <p>I searched a lot about this and tried to solve the errors in these ways but nothing works for me</p>
  <ul>
  <li>Clearing data, Reinstalling app...</li>
  <li>Changing version and name of the db...</li>
  <li>Calling onCreate in onOpen...</li>
  </ul>
  <p>and some others like this, But no trick works</p>
  <p>This is my entire database helper class (after modification)</p>
  <pre><code>class LocationDB(private val myContext: Context) {
      private var dbPath: String? = null
      private var db: SQLiteDatabase? = null
      private var dbHelper: DatabaseHelper
  
      inner class DatabaseHelper
          (context: Context?) : SQLiteOpenHelper(context, DB_NAME, null, 5) {
          override fun onCreate(db: SQLiteDatabase) {}
  
          override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
              if(newVersion&gt;oldVersion)
                  copyDataBase()
          }
  
          override fun onOpen(db: SQLiteDatabase?) {
              if (db != null) {
                  onCreate(db)
              }
          }
      }
  
      @Throws(IOException::class)
      fun createDataBase() {
          val dbExist = checkDataBase()
          if (!dbExist) {
              dbHelper.readableDatabase
              try {
                  copyDataBase()
              } catch (e: IOException) {
                  throw Error(&quot;Error copying database&quot;)
              }
          }
      }
  
      private fun checkDataBase(): Boolean {
          var checkDB: SQLiteDatabase? = null
          try {
              val myPath = dbPath + DB_NAME
              checkDB = SQLiteDatabase.openDatabase(
                  myPath, null,
                  SQLiteDatabase.OPEN_READONLY
              )
          } catch (e: SQLiteException) {
          }
          checkDB?.close()
          return checkDB != null
      }
  
      @Throws(IOException::class)
      private fun copyDataBase() {
          val myInput = myContext.assets.open(DB_NAME)
  
          val outFileName = dbPath + DB_NAME
  
          val myOutput: OutputStream = FileOutputStream(outFileName)
  
          val buffer = ByteArray(1024)
          var length: Int
          while (myInput.read(buffer).also { length = it } &gt; 0) {
              myOutput.write(buffer, 0, length)
          }
  
          myOutput.flush()
          myOutput.close()
          myInput.close()
      }
  
      @Throws(SQLException::class)
      fun openDB(): LocationDB {
          db = dbHelper.writableDatabase
          return this
      }
  
      fun closeDB() {
          dbHelper.close()
      }
  
      val countries: Cursor?
          get() {
              val query = &quot;SELECT * FROM countries&quot;
              return db?.rawQuery(query, null)
          }
  
      fun getStateAsPerCountry(country_id: String): Cursor? {
          val query = &quot;SELECT * FROM states where country_id=$country_id&quot;
          return db?.rawQuery(query, null)
      }
  
      fun getCitiesAsPerState(state_id: String): Cursor? {
          val query = &quot;SELECT * FROM cities where state_id=$state_id&quot;
          return db?.rawQuery(query, null)
      }
  
      companion object {
          const val DB_NAME = &quot;Location.db&quot;
      }
  
      init {
          dbPath = myContext.filesDir.path + myContext.packageName + &quot;/databases/&quot;
          dbHelper = DatabaseHelper(myContext)
      }
  }
  </code></pre>
  <h1>Edited (10/06/2021)</h1>
  <p>I see <em>Device File Explorer</em> and I get that the path of the database is /data/data/com.my.package/databases/Location.db by default. And when I debug the app at the error breakpoint, I see that the path is different or we can say that the database helper is getting the database from different path /data/user/0/com.my.package/databases/Location.db which does not exist, So it's clear that without exact path the table will always be null or not created. But the question is why and how can we initialize the right path.</p>
  <p>For that, I did this and get the exact path but it is still not working</p>
  <pre><code>val countries: Cursor?
          get() {
              db = SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READONLY) 
              val query = &quot;SELECT * FROM countries&quot;
              return db?.rawQuery(query, null)
          } 
  </code></pre>
  <p>This is a picture of the database</p>
  <p><a href="https://i.stack.imgur.com/y1AoD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/y1AoD.png" alt="enter image description here" /></a></p>