2

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.

3
  • 1
    Please include your logcat output in the question. If you're new to SQL or SQLite, I'd suggest you look into Room Persistence Library, which is safer, less error-prone and easier to use. Android Developer video about it here. Commented Apr 5, 2019 at 13:55
  • @MDNaseemAshraf It makes me think to CoreData on iOS that I don't like... Too much code, even for simple SQL queries. I prefer to let the power to the SQL Commented Apr 5, 2019 at 14:08
  • I did not get you. Room is easier than SQLite. Setting up SQLite is more verbose (lots of boiler plate code) and has a possibility of error creeping into it. Room instead checks for error and issues before and during compilation, plus it's a Data Object Mapper making it easier to use. It's preferred by Google against direct use of SQLite; Read here: developer.android.com/training/data-storage/sqlite Commented Apr 5, 2019 at 14:21

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Interesting, it seems to be what I want. I will tell you.
1

With SQLiteOpenHelper, you need to write code in the onCreate() override to execute SQL that creates the tables you want. Your implementation just prints the database name, leaving you with an empty database and no tables.

After adding the SQL there, you can uninstall your app once to remove the old empty database and make onCreate() trigger again.

2 Comments

Because I don't create the table, I use a filled database SQLite. May be it is not well copied?
Most attempts at such I’ve seen on SO have been wraught with many bugs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.