Home » Android » database – Can I download an SQLite db on /sdcard and access it from my Android app?

database – Can I download an SQLite db on /sdcard and access it from my Android app?

Posted by: admin April 23, 2020 Leave a comment

Questions:

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.

How to&Answers:

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());

Answer:

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());

Answer:

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

Answer:

check this out …

storing android application data on SD Card

Answer:

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();