34

I already found out that there is no way to bundle files in an .apk and have them on /sdcard, the best option so far being to download the large files upon first run. I came accross a tutorial saying how to bundle an sqlite db with the apk and then copy it so that it can be accessed with SQLiteDatabase (thus doubling the space needed, and not using /sdcard at all).

http://developer.android.com/guide/topics/data/data-storage.html#db says all databases MUST be in /data/data/package_name/databases.

Is that really so? Is there a way to trick the framework into opening a database that is placed on the /sdcard partition? Is there a way to use another SQLite java wrapper/framework to access such databases?

If the answer to the above is 'No', what other options do I have? My data is very well represented in a relational model, but is just too big, plus, I want to be able to update it without the need to reinstall/upgrade the entire app.

kzyapkov
  • 627
  • 2
  • 8
  • 11
  • 1
    You mentioned "I came accross a tutorial saying how to bundle an sqlite db with the apk and then copy it..". Do you have a pointer to this resource? – Ichorus Nov 04 '10 at 22:37
  • 1
    @Ichorus - I think this is what he might be referring to - http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/ – Abhinav Gujjar Nov 17 '12 at 15:05

5 Answers5

44

Sure you can. The docs are a little conflicting about this as they also say that no limitations are imposed. I think they should say that relative paths are to the above location and dbs there ARE private. Here is the code you want:

File dbfile = new File("/sdcard/mydb.sqlite" ); 
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);
System.out.println("Its open? "  + db.isOpen());
AdamC
  • 16,087
  • 8
  • 51
  • 67
  • 2
    Then can we access that database file from all activities in out application??? I was not able to do tht conventionally!! –  Feb 03 '11 at 17:23
  • 1
    Thanks Adam. And don't forget to add this permission: in your AndroidManifest.xml – Ashok Goli Nov 25 '11 at 20:23
  • @Ashik: I don't see why you can't access it in your other Activities. We have many options - transfer the opened SQLiteDatabase db object to your required Activity or store your db object in a static reference. That should cover it up. – Ashok Goli Nov 25 '11 at 20:26
  • It doesn't work, I had to copy database to /databases(internal) folder to make it work – Ali Asheer Dec 29 '16 at 12:09
7

Try this:

String dir = Environment.getExternalStorageDirectory().getPath()
File dbfile = new File(dir+"/mydb.sqlite"); 
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);
System.out.println("Its open? "  + db.isOpen());
Mike Aski
  • 9,180
  • 4
  • 46
  • 63
Iwan
  • 71
  • 1
  • 1
1

I share next code. Declare opcionesMenu Vector<String>

Vector < String > opcionesMenu = new Vector< String >();
// the database is SDCard. I saw  the code to Blackberry (net.rim)
String rutaDB = "/storage/extSdCard/Android/databases/Offshore.db";


    SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(rutaDB, null);

    Cursor cursor = db.rawQuery("SELECT Codigo, Nombre FROM Ruta ORDER BY Codigo, Nombre", null);

    if (cursor.moveToFirst())
    {
        do
        {
            opcionesMenu.add(cursor.getString(0) + " - " + cursor.getString(1));
        } while(cursor.moveToNext());
    }

    db.close();
Kiquenet
  • 14,494
  • 35
  • 148
  • 243
Sergio
  • 11
  • 1
1

Just an idea:

  • you can put your database.db into the assets folder.

  • if your database.db file is larger than 2Mb the system is unable to compress it, so you need other one options

  • you can rename your database.db for example database.jit or database.mp3 - which are not compressed, than at the first run you can rename it to database.db

Barna
  • 113
  • 1
  • 1
  • 9
1

check this out ...

storing android application data on SD Card

Community
  • 1
  • 1
Rabih harb
  • 1,372
  • 12
  • 11