Streetboy
Streetboy

Reputation: 4401

How to save sqlite database in resources folder android?

Hello i am following this tutorial Using your own SQLite database in Android applications

Now i don't understand how is save in first place created database. Here is the code how sqlite is taken from resources folder:

InputStream myInput = myContext.getAssets().open(DB_NAME); 

So where to keep in first place my sqlite as if i copy in resources folder i get compiler error:

invalid resource directory name

Thanks.

Upvotes: 0

Views: 2828

Answers (2)

Rithesh M
Rithesh M

Reputation: 1287

In your Activity.java you need to setup your DB

 private void setupDatabase() {
                Dbhelper myDbHelper = new Dbhelper(getApplicationContext());
                myDbHelper = new Dbhelper(this);

                try {

                    myDbHelper.createDataBase();

                } catch (IOException ioe) {

                    throw new Error("Unable to create database");

                }

            }

Upvotes: 1

waqaslam
waqaslam

Reputation: 68177

As what i did in one of my project, copy your database file in raw folder and get its InputStream as below:

InputStream myInput = myContext.getResources().openRawResource(DB_NAME);

now copy myInput stream contents to a cache directory in order to create a copy of your database file.

Further you may use that database file with SQLiteDatabase.openDatabase to get SQLiteDatabase object and do your sql magic

Upvotes: 1

Related Questions