I already have a sqlite3 db file that I created in Windows, is there a way to package this file into my android application and access it as a sqlite db from within the application or do I have to create a db on application load or something in Android only?
The db has about a thousand records, so writing sql scripts again might be monotonous and cumbersome.
Got this link, seems to serve my purpose
Package it as an asset or raw resource. Copy it out from there onto the appropriate spot on the device. Downside: You will take up 1.5x to 2x the space on the flash (for the possibly-compressed copy stored in the APK plus the actual to-be-used copy).
Or, download the database from a server on first run.
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 a question regarding creating an android application for travel guide. I am creating it offline. I am familiar with the problem that we access the database on the real device. I want an external database file in which the data is already filled. I just want to include this database in my apk and just want to access that database in my phone offline. I am using SLQite database. I had imported db file from assets folder. It works on emulator but not on a real device due to no access to system files.
I think this blog post does what you want to do. I am using this approach in my applications for a while and I am satisfied with it.
It is fast and relatively easy.
http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/
I have included the db in assets folder and the db file is copied in the path sdcard/Android/data/packagename/test.db on a real device..now I am accessing the db, it works fine on emulator but when I used it on a real device it get crashed.the db is still not working on real device.
I want to know how to use A DB MANAGER (GUI tool) for built-in SQLite db in Android.I mean,I want to create,edit,delete,insert table/data by DB MANAGER rather than hard coding it, then I just want to read the data from DB. Can I export/import DB to/from android built-in DB. Why can/should I NOT use any independent,preloaded database in my application?
You can build your DB outside of the app and then load it in. No reason not to do so. You just need to make sure to set it up so that it meets certain requirements needed by Android. Those requirements would be:
The primary key for each table needs to be "_id".
The DB must contain a table called "android_metadata"
The "android_metadata" table must contain a field called "locale"
The "locale" field must have an entry (for the USA it needs to be "en_US")
Once the DB is set up you have a choice. Either put it in the assets folder of your app so it will be compiled into the app, or you can set up code to fetch it through some web service. In either case, it needs to end up in your apps data/databases directory so it can be used by the android system (if you want it kept private/unusable by other apps).
A good article covering what I've said above (as well as delving into code to copy the DB from your assets folder to the databases directory) can be found here.
Edit
You cannot put the DB directly into your apps data directory as its blocked from access by anything except your app (unless you root your device), so you must put it in your assets folder and then copy it from there to your apps data directory (or somewhere else that it can be used... it cannot be used from your apps assets).
The standard directory for DB storage is different for each app, there is no single directory for all of them. Android uses /data/data/your.package.name/databases/.
There are many ways to get the data from your DB... anything from copying it to a textfile and emailing it to wholesale copying the DB to/from your SD card where any other app can access it or you can with a DB manager.
I have an SQLite3 Database (created in a desktop sqlite application), with the android_metadata table for use in my android application.
What is the best way to create the android database using this database file?
I have tried including it in the assets folder, but got a size error when copying this to the application and wasn't sure if this was due to the asset files having a maximum size.
I have also read guides on storing the database on the sd card and accessing it.
Which function on androids sqlite helper is best to open a new database from an sqlite3 export?
I pushed the database file to data/data/com../databases/ and it created an "android.database.sqlite.SQLiteDatabaseCorruptException: disk malformed..
Not sure how to do this, appreciate any help!
You can attempt to use SQLiteAssetHelper for this, as it is designed to make it easy for developers to package a database with their app.
That being said, your database is much too big. There are plenty of 1.6 devices that will not have room for both your APK and the unpacked database.
The reason you get a size error is because android likes to compress files in the Assets folder but then can't decompress them if they are over 1 meg in size. Some file extensions do not get compressed like jpeg, gif, mp3, jet. I always name my db something like name.db.jet and then rename it to name.db when I copy it to the Database directory. The extension on the file really means nothing but naming it with an extension like .jet gets around the max size restrictions.
By the way, if you are going to include a DB that is 30mb in size, be sure to include it compressed. That will take it down to about 10mb in size.
i have created a db in the phone and have inserted some values in it as well. now there is a large excel sheet which contains several more records that need to be inserted.
i am trying to find the db file in the phone so as to use sql lite db tool to upload some data in it. but the folder is empty.
can any one suggest or guide.
thanks
If you are on a real phone the /data folder will appear empty unless you have root access. An application like root explorer can allow you access to these folders (with the proper permissions). This probably isn't what you want to/should do.
If you are on an emulator you can use the file explorer in eclipse to browse your phones data/data/package folders and copy your database to and from the emulator quickly (or use adb shell commands).
Are you just doing this to test something? You should have your application insert the data you want, or create your own SQLite database and package it with your application. If you externally modify your database like this, reinstalling the application - or installing it on a different device - would result in an application still using your old/unmodified database.