I'm writing my first serious Android App, which basically is interface to three DB tables.
Data in those tables are predefined by me, so, user should install those app with those data.
What is the best way to include those data in application package? Maybe there is a way to embed SQLite into my application distribution?
Or is the only way is to define array of "insert into" strings somewhere in class and execute them to fill internal SQLite storage?
Would appreciate any recommendations.
I am currently doing the same thing in my app. Having a sqlite database file in my assets folder and copying it into the SD card at startup if it's not in there. There's a nice tutorial there and I use part of its code.
Put the database file in your assets and then copy it over to your application's data directory.
Your can check out this tutorial
Related
In my iOS app I have a large (270MB), pre-populated, read-only sqlite database. I keep the data in the app bundle and query it with no problems. I do not copy the database to the user's documents folder, because it would be pointless in this situation to take up more space with a duplicate database. I have a separate much smaller database I copy to the user's documents folder to store the user's preferences. The app works just fine.
Now I'm trying to port my app to Android using Android Studio, but it does not seem possible to access the database from the assets folder. I have found plenty of documentation on database helper classes for Android, which I have tried, but the approach always seems to be to copy the database from the assets folder to the user's data folder. This would be a waste of space and also in my experience the app is unable to copy the database without crashing (maybe because of the size? I had no problems copying a smaller test database).
Is there a way to access the database without copying it to the user's data folder? If not can anyone think of another way of approaching this?
No, You can not directly write into the files of ASSETS folder in your application as the resources folders are read-only.
So You have to compulsory copy your database first from your assets folder to your sdcard and then only you will be able to read & write into it.
As GrIsHu said, you can only read database from asset folder. If you need to do more operation like create, update, delete you can do a tricks. Copy the database from assets folder to storage and then you can do anything you want.
Here is a quick example of Working with Android Pre Built Database.
There is a easy to use library too for accessing database from assets folder. You can check [Android SQLiteAssetHelper] here.2 Good luck!
I have created my database in NAVICAT FOR SQLITE, now i want that database to be connected with my android Application.
I have surfed many webs but didn't found anything as a solution to my problem.
i don't know what will be the exact code for it?
However an additional feature i have to overcome is to handle some events like, if I update something in my Application, it should also be updated in my database which is currently in my desktop application.
I think it needs a WiFi connection for it?
Can some one guide me regarding this?
there's two options for ur problem..
keep that database in assets folder and after starting the application copy the db from assets into application package.. refer this http://iamvijayakumar.blogspot.in/.../copy-sqlite...
keep that database on ur internal storage or sd card.. and give the full path as database name.. it will work...
Recommended: use 1st option..
I am developing an app in android which consists of activities that need to connect to the Database . I added my database file in assets folder which gets copied over to applications database directory on first time the app runs but "assets" directory and "data" directory(on rooted devices) can be accessed by any other application . I'm confusing between using database file or create database in code . If i create database in code it make the database file disappear in the "assets" folder . When users change the file extension from .apk to .zip ,database file will not appear in assest folder . What I should to do ?
Please give me some advice !
Both ways are good and useful it completely depends on your need.
By creating database in code you can secure your data from other applications but it will take so much pain to create it in that way so i suggest you to use a db or sqlite file in assets folder and while copying database on device or data folder use some security parameters to encrypt it or you can hide your app database folder on device so other applications and users will be not able to access it easily.
Well keeping Database in assets folder is not at all a bad practice plus it saves coding of creating a database , as far as you want to make it secure you have to do 2 things
1.keep you database in assests folder , and copy and save it in the internal memory , now its available only to your application and delete it from assests folder .
2.Use Proguard to protect it from somebody decompiling your application and obtaining the assests.
And yea if its a confidential data in the application and your application is worth it then you can also go for "encrypting data" but yea its a TDS task , see for yourself what suits you now.
Honestly, this is the best explained and a very complete tutorial on the subject
SQLite Android Tutorial
Don't get repulsive, because it's a bit longer one. Everything is explained nicely and you don't have to do the thinking about the location of your asset folder on different devices and so on...
Pretty simple question, can I send a already created database (a file, for instance) with my application?
Or do I have to create it inside my application?
Yes you can do both. Either create on 1st launch or copy preconfigured database from assets.
For the latter option (which is basically significantly faster if you need your database to be pre-populated) you can use Android SQLiteAssetHelper to do all the job.
you may put it into the assets/ folder and copy into the SQLite on the first run.
Some apps include an SQLite database in the assets folder, and copy it when the app is launched.
I am using java methods to copy my application sqlite db from the application data directory to a folder in sd and have code to restore these file back to the application data directory. Is this a proper way of doing it? Are there potential errors or data corruption since it's a database file?
It's perfectly fine to do so. SQLite files do not have any dependencies to other files, so copying them to SD card will achieve your goal. You can check the contents of SQLite files with SQLite browser if you want to make sure.
I would suggest exporting your DB to JSON format, and save that on an external location for backup/restore. That way, you can control the data you backup (maybe you want to skip backing up some confidential data?) and you can allow advanced restore features (restore only the data and not the preferences).
In order to do that, you need to represent every table with a class (if your using ORM - this is needed anyway), and then just use one of the JSON libraries, like "Gson", to get the JSON representation of the DB. After that, you can save it to some file wherever you'd like.