Can I store multiple databases in an application's database folder? - android

I was having an SQLite database with size less than 1 MB in the asset folder. I am copying this database to the applications default database folder on application launch. It was working fine. Now the database has grown and due to limitations with the Asset folder, I am not able to copy the database from Asset folder to the application.
Is there any way to manage multiple databases in application? If yes, what will be the methods to query them separately? Please share some good link for such database queries.

Change your db file's suffix to media file type, such like yourdb.mpg . then you won't be limited by 1mb size, and you can copy it to data/your.packge/database folder when your app first run.

Related

Backup SQLite database and restore it back when the Android application is installed

I am building an Android application that fetches data from a cloud database and stores it in SQLite locally so that user does not need to fetch it again and again.
Now I need to find an efficient way to predefine a few rows in the SQLite database and provide it along with the APK. Is this possible? if so, how do I achieve it?
Yes it is possible.
You follow these steps :-
You create the database externally, populating it, and copy the file to your App's assets folder.
You may have to create the folder.
If using Android SQLiteAssetHelper then you will need to create a databases folder in the assets folder.
There are various tools for Creating and Managing SQLite Databases. e.g. Db Browser for SQLite.
You then need to modify your App to copy the file from the assets folder (or assets/databases folder) and then open the database. Noting that you only do the copy if the database doesn't already exist.
Using the Android SQLiteAssetHelper simplifies this process.

Accessing SQLite database from Assets Folder without copying to user's data folder

In my iOS app I have a large (270MB), pre-populated, read-only sqlite database. I keep the data in the app bundle and query it with no problems. I do not copy the database to the user's documents folder, because it would be pointless in this situation to take up more space with a duplicate database. I have a separate much smaller database I copy to the user's documents folder to store the user's preferences. The app works just fine.
Now I'm trying to port my app to Android using Android Studio, but it does not seem possible to access the database from the assets folder. I have found plenty of documentation on database helper classes for Android, which I have tried, but the approach always seems to be to copy the database from the assets folder to the user's data folder. This would be a waste of space and also in my experience the app is unable to copy the database without crashing (maybe because of the size? I had no problems copying a smaller test database).
Is there a way to access the database without copying it to the user's data folder? If not can anyone think of another way of approaching this?
No, You can not directly write into the files of ASSETS folder in your application as the resources folders are read-only.
So You have to compulsory copy your database first from your assets folder to your sdcard and then only you will be able to read & write into it.
As GrIsHu said, you can only read database from asset folder. If you need to do more operation like create, update, delete you can do a tricks. Copy the database from assets folder to storage and then you can do anything you want.
Here is a quick example of Working with Android Pre Built Database.
There is a easy to use library too for accessing database from assets folder. You can check [Android SQLiteAssetHelper] here.2 Good luck!

Why can't we read an sqlite file from /raw or /assets?

I have an sqlite database I want to distribute with my application. It's about 3mb. If I understand correctly, I can't directly open it if it resides in my app's /raw or /assets folder, right? We need to first copy it out of that directly as in:
Get the SQLite path within the Assets Folder
So my app will consume twice the disk space as it normally would? I don't understand why it's necessary to have the database file be in a particular folder?
Thanks
So my app will consume twice the disk space as it normally would?
Yes. Technically, probably more, as your database is compressed inside the APK file.
I don't understand why it's necessary to have the database file be in a particular folder?
It is not necessary for the database file to be in a particular folder. However, assets/ is a folder on your development machine; it is a part of the APK file on the device. Same goes for everything in res/raw/.
BTW, I recommend SQLiteAssetHelper for copying the database out of assets/.

Open sqlite in readonly directly from assets

I've found a lot of solutions to about how its possible to copy a database from assets to /data/data/tld.c.u/databases/ - But is it absolutely impossible to just open the database in readonly and query its content?
I have a rather large database i'm distributing with the app and its kind of a waste if the database must be copied out of the assets folder.
But is it absolutely impossible to just open the database in readonly and query its content?
Yes.
I have a rather large database i'm distributing with the app and its kind of a waste if the database must be copied out of the assets folder.
Consider not distributing it with the app, but rather downloading it on first run, or using Google Play's new APK expansion files facility.

30mb Sqlite3 Database - Including in android app

I have an SQLite3 Database (created in a desktop sqlite application), with the android_metadata table for use in my android application.
What is the best way to create the android database using this database file?
I have tried including it in the assets folder, but got a size error when copying this to the application and wasn't sure if this was due to the asset files having a maximum size.
I have also read guides on storing the database on the sd card and accessing it.
Which function on androids sqlite helper is best to open a new database from an sqlite3 export?
I pushed the database file to data/data/com../databases/ and it created an "android.database.sqlite.SQLiteDatabaseCorruptException: disk malformed..
Not sure how to do this, appreciate any help!
You can attempt to use SQLiteAssetHelper for this, as it is designed to make it easy for developers to package a database with their app.
That being said, your database is much too big. There are plenty of 1.6 devices that will not have room for both your APK and the unpacked database.
The reason you get a size error is because android likes to compress files in the Assets folder but then can't decompress them if they are over 1 meg in size. Some file extensions do not get compressed like jpeg, gif, mp3, jet. I always name my db something like name.db.jet and then rename it to name.db when I copy it to the Database directory. The extension on the file really means nothing but naming it with an extension like .jet gets around the max size restrictions.
By the way, if you are going to include a DB that is 30mb in size, be sure to include it compressed. That will take it down to about 10mb in size.

Categories

Resources