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.
Related
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.
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.
This question already has answers here:
Ship an application with a database
(15 answers)
Closed 8 years ago.
i created my first app and I would ship it with a local database that contains about 600 entries.
I tried to hard-code this data in my class but i think it is not a good idea.
So is it possible to copy a database to phone on first start of an application?
Store the file in assests folder or in the xml format in res folder.During start up you can copy to the database.
Rather than hardcoding.You can place that .db file in the assests folder of your application's project.Then by using simple File IO of java.You can copy the file from assests and place it in the respective folder from where you can have access to it in your application.
See this link
It mostly depends on your application type, and the sensitivity of the data, some of the possibilities I can think of:
Adding a raw SQL text file with all the values to your project. Then you check every time in Application.onCreate() if the database exists. If not, you run the import job which runs the SQL on the newly created database.
Same as the first one, but you download the SQL file over https, ensuring that decompiling the application does not leak the information. You need to ensure the user has an internet connection though.
If you prefer using database dumps, you can also import (and export) SQLite dumps. Then you could use the methods described here to set it up in your app. I would not recommend it, because fixing small things is done easier in SQL than in dumps.
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.
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.