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.
Related
I'm trying to implement a simple pre populated database into my assets folder of my Android application which I will then query at a later point. But the problem I'm facing is an encoding issue which I can convert the database inside of Android studio but then it implements a password using cipher encryption.
My current process of adding the database to my project
Firstly I went through the process of building a simple database and table inside of the sqlitebrowser application and made sure that the encoding within the preferences section was set to UTF-8.
http://sqlitebrowser.org - Link to the application which I'm currently using to build the database.
After I was happy with the database I then simply copy and past it into my assets folder.
I'm now presented with the image below (example)
4.My next step is that I convert it to UTF-8 as requested but I thought the sqlite browser would of already put it in this format.
5.The Code will now build the database correctly and I can see it within my data folder.
I then take a copy of the database off the virtual device, but when attempting to open it using sqlitebrowser I'm presented by a window asking for a password (Cipher Encryption). Now when I first built the database at no point did I add a password, so this makes think when Android asked to covert it did it also implement encryption?
Going back to the main question, how can I build a simple database in UTF-8 format which I can then implement into Android Studio assets folder?
At step 2 STOP ignore steps 3 on.
Instead step 3 write an application (perhaps using Android SQLiteAssetHelper, there are numerous tutorials on using SQLiteAssetHelper) to open and use the Database.
Android Studio is not designed to browse/interogate SQLite files, so basically it doesn't have a clue what the underlying data means. However from your screen shot you can see that some of the data does in fact indicate an SQLite file.
For example the following is a screen shot of opening an SQLite Database, (actually created and used in an Android App) in Notepad++ (see any similarities?) :-
e.g. It starts with SQLite format 3, the below mentions tablestudent and then goes on with CREATE TABLE students (_id......
So in all likelihood the data is fine it just needs to be opened/used by something that knows how to interpret the data in the file i.e. SQLite.
What can be seen from your screen shot, is that in all likelihood you have a column named _id.
Therefore your an answer to your question (ignoring opinions such as the best) :-
Hello Zoe what is the best application for building a simple sqlite
database which will be in UTF-8 format that I can then import into my
assets folder?
is, it looks as though you have a suitable tool already (personally I use SQlite Manager for maniuplating and interrogating SQLite Databases, can't say whether SQLite Brwowser is better or worse or the same). All you need to do now is ascertain how to do the new step 3.
regarding your statement
I then past the database into my assets folder which means 3 must be
an image from the assets folder.
That's not the case, all it is is that Android Studio has made an attempt to open the file and perhaps believes that it should be an image. I suspect you could drop in a PDF or Word document (might have to change the extension to fool Android Studio) and it may well come up with a similar response. i.e (not that I've tried this) it may be that Android Studio has not been designed to read in MS Word or PDF documents, so knows nothing about them.
Ok, tried this now, here's part of a PDF file (extension renamed to xxx, as AS opened it as a PDF with pdf extension) :-
I am making an Android version of an iOS app that I already have. I want to use a local database so that the user can use most the app offline.
Initally I thought I could use the sqlite database created by Core Data in Xcode, but as I am reading things online it seems like this is not possible. Is this true? Or is there a good way to export it to something Android could use?
If not, I want to create a local database with values from a database on the cloud(I use Parse.com). How can I do this? The data on the cloud doesn't change very often (maybe twice or thrice a year) if that makes any difference.
This is a good tutorial to handle preloaded databases:
http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/
Essentially, once you have your precreated database, put it in your assets directory in your apk. Then on first app use, copy this from assets to "/data/data/YOUR_PACKAGE/databases/" directory.
You have to create the database by code in Android, using a DatabaseHelper http://developer.android.com/guide/topics/data/data-storage.html#db. If you want to use your already created database you will have to export it to SQL statements and then "import" them to your application.
This question already has answers here:
How to use an existing database with an Android application [duplicate]
(5 answers)
Closed 9 years ago.
I have a sqlite file with an extension of (db_name.sqlite) and i want to reuse this database in my android project instead of creating new one because, this database has huge data stored in it.
How is it possible?
If your "huge data" is less than 50MB, you can package the database as an asset and then unpack it on first run of your app. I recommend SQLiteAssetHelper for this.
Once your APK grows to 50MB, though, things get a bit more complicated, as you would need to either download the database yourself or look into APK Expansion Files.
Once you have the SQLite database in place, you can use it the same way as you use any other SQLite database in Android. SQLiteAssetHelper will offer the same getReadableDatabase() and getWriteableDatabase() methods you see in SQLiteOpenHelper, giving you a SQLiteDatabase object on which you can run queries, etc.
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.
Basically, I'm trying to store some data (~300 rows, ~10 columns) for an android app. This data will not be changed by the app, just used in calculations and stuff.
However, everything I've found online (example) talks about using a database that is created at runtime. I do not want this, the data will be the same every time the app is run.
So is there a way of using a databse like this? (Or any other way of storing data in a table-like fashion?)
Generate the SQLite database as part of your build and keep it in your app's raw resources. Since all you need is the file's path and name to open it, you can still read it fine. Your open helper will still go through onCreate() the first time unless you include the table Android uses for its own bookkeeping, but that should be okay.
Make sure you only open it for reading, and you should be good to go.
Put your custom file in the assets folder under the project root.
To get a inputstream from the file, just do:
context.getAssets().open(file);
In this way you can store your static data in conma separated or any model you want.
If you want the data constantly changing, you can create a temporary file in the SDCard, by accessing and creating a new file under some path at:
Environment.getExternalStorageDirectory()
How about storing this data in raw file under Assets or Res/raw folder. You can either dump this data on the fly in Database or read it and process it [which may be costly]. Dynamic handling may be costly, test it and compare performance.