Accessing SQLite Database Through Assets - android

Im trying to use ionic with the SQLite plugin (https://github.com/brodysoft/Cordova-SQLitePlugin) and I was able to create and use a database, but in my app i need a prepopulated database.
I see methods of achieving this by placing the database in the platforms/android/assets folder in the ionic project, and then copying it, on the first android run, to the "correct location".
My question is, why is it needed to copy it to another location? why cant I just access it from the assets folder which the application creates? If it was an image, i wouldnt need to change its place either, i would use it from the assets, so why not the db too?

Your database file in the assets folder is stored in an exported file format (minimum storage space required, but not for interactive use). The Android SQLite Database needs to import this file. This means reading and storing it in a new format for better searching/reading and writing.
So it is not a simple copying process it's an interaction and after the import your database does not read from your assets folder any longer. So replacing your database in your assets folder does not update your imported database.
You can use this project to import your database in android: https://github.com/jgilfelt/android-sqlite-asset-helper

Related

Does an app recognise database files that were created by other projects?

I have two projects, in the first one i created a database successfully. My question is: is an app capable of processing this database if i copy the .db file from the first project to the location where all android apps save their databases
/data/data/pkgname/databases/yourdatabase.db?
or there would be some missing files?
I think the best solution is to move your database file in public directory after creating it, like sdcard.

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!

Is it possible to import a DB from a .sql file in Android?

The application that I am creating will have a database that will have questions and answers. My problem is that I will be required to make hundreds of inserts to make the database complete. What is the best way to make it possible to do hundreds of insert statements. I am also thinking about to creating the database through the SQlite manager and from it to export a .sql file and use it to create a DB for my application.
If you have static data inside your database the it would be better to create the database from SQLite Manager and keep it inside assets folder and then copy it from assets folder into your databases directory when your Application starts. Also check the database file if exists the don't copy else it will overwrite the previous file everytime when you start Application.

How to update Sqlite DB in asset folder, not the copy DB?

I need to deal with sqlite db placed in asset folder in Android project.
The issue is that usually we create a copy of asset db at runtime and all the queries are done on this copy, but the problem is that when I clear the cache it will remove all updates; so I need to update the asset file not the copy that has been created on runtime.
Can I insert update delete the asset sqlite file?
Actually I want to preserve modifications on DB, and I don't want to loose these changes when clearing application data from settings.
You CANNOT modify resources (assets, raw, drawable, etc.) of an .apk once its built. They are read-only, so what ever goes-in will always remain as it is - until you update the .apk with the newer stuff.
You can not get the old record once you cleared the cache, and cant even update the asset file, once the apk file is generated as it is READ-ONLY. Yeah if you want assistance on how to use already ready database then this link will help you for sure.
Using your own SQlite Database in android applications
And i think if you can copy database from assets folder, then, you can get it from sdcard too. dont know much about it.

Categories

Resources