SURYA KARUTURI
SURYA KARUTURI

Reputation: 119

access .db file from assets in android

i have a .db file with 2MB of size. i put it in assets folder in my project. And wrote the code as follow.but i got exception no table exits.please help me How to access .db file without copying into /databases.

db = openOrCreateDatabase("assets\\TDP.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);
    db.setVersion(1);
    db.setLocale(Locale.getDefault());
    db.setLockingEnabled(true);
  //  db.execSQL("create table TD_ASSEMBLY(name Text,password Text);");
  Cursor cur = db.query("TD_ASSEMBLY", 
            null, null, null, null, null, null);
       cur.moveToFirst();
       while (cur.isAfterLast() == false) {
         String first= cur.getString(0); 
         String second=cur.getString(1);
         tx.append(first+"  "+second);
         cur.moveToNext();
       }

Upvotes: 1

Views: 4369

Answers (2)

Jaydeep Khamar
Jaydeep Khamar

Reputation: 5985

refer these links' answers to copy database from assets:

adding your own SQLite database to an android application

Database not copying from assets

Upvotes: 0

Aleadam
Aleadam

Reputation: 40401

Don't use a regular path to retrieve a file from assets. Use getAssets() instead.

The best option is to save it on first run to the database folder in your app. See my answer here: The data disappear after moving the application on a real device in android and the link therein for an example.

Upvotes: 2

Related Questions