Search code examples
androidkotlinandroid-sqlite

Kotlin/Android: android.database.sqlite.SQLiteException: no such table: Table_xx (code 1 SQLITE_ERROR): , while compiling: select * from Table_xx


I have a problem to access to my table, but impossible to know where is the problem. The table exists well in the SQL select * from Table_xx.

I don't know if the no such table error message is really a problem of missing table...

class DBLocal(context: Context, name: String?, factory: SQLiteDatabase.CursorFactory?, version: Int) : SQLiteOpenHelper(context, DATABASE_NAME, factory, DATABASE_VERSION)
{

    override fun onCreate(db: SQLiteDatabase)
    {
        print(DATABASE_NAME)
    }

    override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int)
    {
    }
    fun methodToSelectData(strQuery: String)/*, completion: @escaping (_ result:*/
    {
         print("test")

        val list = ArrayList<String>()
        val db = this.readableDatabase


        val c = db.rawQuery(strQuery, null)

        if (c.moveToFirst()) {
            do {
                list.add(c.getString(c.getColumnIndexOrThrow("lng")))
            } while (c.moveToNext())
        }
        c.close()
        db.close()
    }

    fun methodToInsertUpdateDeleteData(strQuery: String)/*, completion: @escaping (_ result: Bool) -> Void)*/
    {

    }
    companion object {
        private val DATABASE_VERSION = 1
        private val DATABASE_NAME = "Local.db"
    }

Thanks in advance.


Solution

  • It is best to create your own database and import from the assets: https://medium.com/@johann.pardanaud/ship-an-android-app-with-a-pre-populated-database-cd2b3aa3311f