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

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.

Related

Push db file to Databases folder in android

I wondered If it is possible to push db files into databases folder of an Android application either programatically or using any tool.
Thanks in advance!
Yes you can but not directly.
Typically a pre-built database would be included as an asset and copied to the databases folder for the package when the App is first run after installation.
If using this method then you should consider using SQLiteAssethelper, as using this simplifies the process.
In one of my App's I introduced a back-restore that backed-up the database and could restore from the downloads folder. If need be I can copy a database from elsewhere into the downloads folder and restore from that within the App. However obviously such a database has to have the correct structure/schema to not result in errors. So that is another way but would be at the user's discretion to do.
Obviously data/data//databases is protected so that imposes restrictions on "Pushing" data directly into the folder.

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!

Accessing SQLite Database Through Assets

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

Android - Best way to using SQLite?

I am developing an app in android which consists of activities that need to connect to the Database . I added my database file in assets folder which gets copied over to applications database directory on first time the app runs but "assets" directory and "data" directory(on rooted devices) can be accessed by any other application . I'm confusing between using database file or create database in code . If i create database in code it make the database file disappear in the "assets" folder . When users change the file extension from .apk to .zip ,database file will not appear in assest folder . What I should to do ?
Please give me some advice !
Both ways are good and useful it completely depends on your need.
By creating database in code you can secure your data from other applications but it will take so much pain to create it in that way so i suggest you to use a db or sqlite file in assets folder and while copying database on device or data folder use some security parameters to encrypt it or you can hide your app database folder on device so other applications and users will be not able to access it easily.
Well keeping Database in assets folder is not at all a bad practice plus it saves coding of creating a database , as far as you want to make it secure you have to do 2 things
1.keep you database in assests folder , and copy and save it in the internal memory , now its available only to your application and delete it from assests folder .
2.Use Proguard to protect it from somebody decompiling your application and obtaining the assests.
And yea if its a confidential data in the application and your application is worth it then you can also go for "encrypting data" but yea its a TDS task , see for yourself what suits you now.
Honestly, this is the best explained and a very complete tutorial on the subject
SQLite Android Tutorial
Don't get repulsive, because it's a bit longer one. Everything is explained nicely and you don't have to do the thinking about the location of your asset folder on different devices and so on...

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/.

Categories

Resources