Best way to populate SQLiteDatabase with initial data? - android

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.

Related

Converting a CSV file into an SQLite .db file

I have a CSV file and am trying to convert it into an SQLite .db file so that I can use it in my Android app.
I am aware of methods I can use within the app to convert a CSV placed in the assets folder to an SQLite database within the Android app (e.g. by reading all lines from the CSV file and adding them to the SQLite database), however I want to generate a .db file outside of the app.
The reason I would prefer to do this is because I have several CSVs at around 5 MB each (over 350,000 rows) and so it would take too long to read them all and put them into a new SQLite database in the Android app.
I'm hoping that being able to put my .db files into the assets folder and using the android-sqlite-asset-helper library to access the data from these will be faster.
I have tried tools like a CSV to SQL converter which uses the data from the CSV file you upload to generate an SQL script (i.e. DROP then CREATE then INSERT), but I'm not sure what I need to do with the .sql file to make a .db out of it.
There are also some ways of doing this (I think) I found on Stack Overflow with commands, but I am unsure of how these work and how I use the commands.
So I am asking how I can convert a CSV file into an SQLite database. What is the best method?
Out of curiosity, have you tried creating the database from within the SQLite CLI? Facilities for CSV import exist: https://www.sqlite.org/cli.html#section_8 followed up with a quick ".save output.db" may accomplish what you need.

Best approach when working with existing SQLite database in StorIO

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.

how to save static database android app on separate folder

I have created a database android app for my college that stores students records and all the record are store in sqlitedatabse and the file name is STUDENT.sqlite.But that file is hidden and can be seen only if phone is rooted.so i need a solution so that the aap gets install it should show separate folder for it and the sqlite file mustbe there in it.so that when i put all records in that and save i can give that file so another person and he can view the same record.no need of entering all records again he can just change the sqlite file.
Maybe you can copy all records from your database into a public or shared directory on sd-card?
Rename the database file to yourdatabase.db and put it in your projects assets folder and on first run of application just copy that database to data directory of android system.

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.

Should I explicitly create SQLite tables in my app?

I am a novice, and am creating a simple app that just reads data from a table in an SQLite database and displays on the GUI. (Just select operation )
During development, I created the database and the table from the tool "SQLite Database browser". And I inserted all data into the table through the tool.
Now, my doubt is ..
1) In java code, should I have some method that creates database and table ?? (For now, I have methods to do the select operation alone)
2) The database I created is located in my local drive. When the apk file gets created would the database also be included in the apk file ??
Pls help !! Thanks in advance !!
1) In Java code, should I have some method that creates database and table ?? (For now, I have methods to do the select operation alone)
No, You have to just copy your database file from /asset directory to /data/data/<package_name>/database directory. And only use select operation alone. You don't need to use create Database and table operation..
2) The database I created is located in my local drive. When the apk file gets created would the database also be included in the apk file?
For this, as I mentioned above you have to first put your database file into application's /asset directory, then copy it to internal storage (when your application start), then it works.
Look at this SO question How to ship an Android application with a database?. It answered what you needed.
try below link
http://www.higherpass.com/Android/Tutorials/Accessing-Data-With-Android-Cursors/

Categories

Resources