Was just wondering if there is a suggested approach when working with a existing data with StorIO?
I will like to ship my app with a lot of existing data. Should I copy my .db from assets to db folder on first launch or is there anything better I can do?
Thanks!
Create prefetched SQLite database.
Put created db file into assets folder.
Write custom SQLiteOpenHelper where you should copy db from assets folder to the db folder of the application data at first time when it's opened. Or you can use Android SQLiteAssetHelper library.
Pass your SQLiteOpenHelper or SQLiteDatabase to the StorIOSQLite.
StorIOSQLite storIOSQLite = new DefaultStorIOSQLite.Builder()
.db(yourPrefetchedDb)
// add types mappings if you need
.build();
That's the easiest way :)
May be you can download data from remote source (server) instead of placing it into the assets?
Because prefetched database will be part of the apk and increase its size and you can not remove this file at runtime.
Related
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.
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
Say I have data about customers and I initially have around 1000 pre-determined customers details I want to insert into my SQLiteDatabase.
Would it be wise to store that data into a text file in my assets folder with my own formatting (tabs to indicate columns and new lines to indicate rows), then just read the text file and insert the data into the database with insert statements? I feel this is not the best way and very in-efficient.
Is there a better way of doing this?
You can use the utility to create your SQL Lite database offline on PC, such as this:
http://portableapps.com/apps/development/sqlite_database_browser_portable
You can package this database along with your app and then put it on phone storage and continue from there.
You can do do in two ways..
Way 1:
You can make one .sqlite file with already 1000 customer records inserted in database and you can use it that .sqlite file from assets folder. Then you can copy those records in your internal database or use that only database.
Way 2:
You can put this 1000 records on server and call it by web service at first time app is launched..
Hope it will help you.
User a .sqlite or .db file in your assets folder with prelaoded entries and import this file to you app from asset folder.
You can use this add-on to access and create you db file
https://addons.mozilla.org/en-US/firefox/addon/sqlite-manager/
and this is tutorial to access database file from assets
Android: Accessing assets folder sqlite database file with .sqlite extension
I would bring along the already pre-populated database .db file.
I've done this before with "sample" data included as part of an application.
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.
I am having Registration screen as my first screen but before that i want to create database. is it possible to create database at time of installation in android rather than creating in first activity.
This is a great article that helps http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/
Create the sqlite db using SQLite Database Browser and store it in your assests folder
When app launches, copy the db from assets to your apps data directory
Open the DB
This way you don't have to parse csv files or dynamically create the db on the device, saving time during first load.
If you want to use a previously created database in your application,at the time of installation the you need to create a database put it in assets folder and then copy it in application.
refer this links:
SQLite Database "context" passed to adapter
Database not copying from assets
adding your own SQLite database to an android application