Ron Gross Ron Gross - 22 days ago 5
Android Question

Android sqlite from pc

I have a pre-baked sqlite database that I want to use in my Android application.

I've followed this tutorial:
http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/

However nothing is throwing an exception during the process of building and copying the db,
but when I access the db tables, they are not there, like they don't exist.

I made this sql query:

database.rawQuery("SELECT * FROM sqlite_master", null).getCount();


and I got the answer: 1. (I have 5 tables)

Any help?

Answer

That tutorial there is too old. Try the following steps:

1) Create a class that extends SQLiteOpenHelper and overwrite the onCreate() and onUpgrade() methods

2) In some Activity instantiate your class, created in 1) and that's it - you will have your database created under data/data/YOUR_PACKAGE/databases/

3) If you want after that, you may obtain writable instance from the class created in 1) (getWritableDatabase()) and execute insert statements towards your database to populate it with data. Wrap your data in ContentValues and use insertOrThrow(your_table_name, null, your_content_values).

Hope that helps.

Comments