How to fill SQLite database in android? [duplicate] - android

This question already has answers here:
Ship an application with a database
(15 answers)
Closed 7 years ago.
I wanna develop an application which has a database which is filled with data. How should I fill the database with data? which of the following should I do?
1- Write a little android app and fill the database and somehow get access to the database file and put that in the app. (I think this is bad)
2- Use some kind of sqlite software to fill the database file and then access the database file and put that in the app.(Again I think this is bad)
3-Any other method.

If the data is not huge, you can download the data from server and fill sqlite database when the user first starts the app.
If the data is huge, you can store the data in a file and put under /res/raw folder of the apk and write code to copy those data to sqlite when the app starts for first time.

Related

Is it a good idea to fill a database using a .txt? [duplicate]

This question already has answers here:
Ship an application with a database
(15 answers)
Closed 7 years ago.
I am currently learning how to use database in Android. For one of my app, I need to stock 500 string values in order to read them.
Actually to fill my database, I read a .txt file at the database creation with all the string values inside. For each lines of .txt (corresponding to a string value), I create a new data in the database. The thing is I know that accessing a file can be long it is not a very good thing if there are thousands of values.
According to that, I have two questions :
Is it really usefull to create a database ? (because I can directly access the string values by reading the .txt)
Is there a better way to fill the database ? Or a way to create a database already filled before the activity creation ? (because currently, each time you close the activity you close the database and each time you reopen it, it recreate and refill the database.)
How about instead of having .txt have a .csv file with values and use org.apache.commons.csv or super csv for parsing the csv file. I haven't used super csv but it provides support for POJO support which i think can be really handy. This will increase your performance and parsing speed. But it would be based on your situation. i would recommend creating a database if you need to perform SQL queries on your data for eg. joins or nested queries. If you just need to display you can use a CSV file.

Android and sqlite, can I populate a database before upload? [duplicate]

This question already has answers here:
Ship an application with a database
(15 answers)
Closed 8 years ago.
I have an app that just need to read from a database. I do not want to parse a big file in the app because it takes a lot of memory.
So is it possible to populate a database and ship it with the app?
Just parsing the file and storing it in the app the first time the app is run will not do since it will create the same memory problem.
An alternative would be to have the database on Google Appengine but I would like to avoid that because the app would be unusable if there is no internet connection and there are costs for the traffic.
So is it possible to populate a database and ship it with the app?
Yes. Create a SQLite database containing your data, then package it with your app using SQLiteAssetHelper. It will be unpacked into position when you first try to use the database.

How to populate SQLite databse with a .txt file? [duplicate]

This question already has an answer here:
Testing a SQLite database on Android
(1 answer)
Closed 9 years ago.
I am writing an android app. I have a txt file with values I need to put in an sqlite database. How can this be done?
Some points:
Include the .txt file as an asset in your app. This will allow you to open it with an AssetFileDescriptor.
You have to read data from the .txt file and insert rows into your database. It's easier to do this if you have some control over the format of your .txt file. For example, if you can you should insert commas between the values, and insert a line feed at the end of each row's worth of data.
Use transactions when necessary.
Do the operation on a background thread. I suggest using an IntentService.

PreLoaded data in SQLite for android [duplicate]

This question already has answers here:
Ship an application with a database
(15 answers)
Closed 8 years ago.
I'm new to android application development. I'm on course of developing my first app which displays the time table of the selected class/faculty on the selected day(Mon,Tue etc.,).
I'm all done with UI. Now my problem is how & where to write the code for the entire database insertion (as it should be loaded from the beginning).
Can any one help me with this in a detailed manner as i'm new to this field ?
The general steps for what you are looking to do are as follows:
1) Create/convert your database to SQLite (the db used by Android)
2) Put the database in your assests folder
3) Write code to copy the DB from your assets folder to the private databases folder for your app when it runs for the first time.
4) Write code to access the database as necessary once the DB is in the private folder.
A tutorial for using/importing an existing SQLite database in your app can be found at http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/
There are many tutorials for creating a database helper class (to handle Creation, Reading, Updating and Deleting functions). A good one I've used and recommended before can be found at http://www.anotherandroidblog.com/2010/08/04/android-database-tutorial/
I think what you need is to ship your database with your apk. You, however, will need to move the database file, because the system expects its databases to be in the private storage. You can see how this is done here.
If it so happens that your database file is big (more than 1MB) combine with this answer in order to achieve the shipment.

Working on sqlite 3 and Android [duplicate]

This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
sqlite example program in android
I've created an sqlite3 database and a number of tables using SQLiteManager 3.5.1. However, i've no idea as to how i should be able to connect, access and use the database. Moreover, i also want to be able to modify the tables and the data in the tables. Is it possible to put the database (with the .sqlite extension) within the assets directory and work back and forth from the android to SQLiteManager?
Yes, it's possible. Here it's explained:
Using SQLite from your Android App
Take a look at the Copying from assets to database path section.

Categories

Resources