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.
Related
I am building an Android application that fetches data from a cloud database and stores it in SQLite locally so that user does not need to fetch it again and again.
Now I need to find an efficient way to predefine a few rows in the SQLite database and provide it along with the APK. Is this possible? if so, how do I achieve it?
Yes it is possible.
You follow these steps :-
You create the database externally, populating it, and copy the file to your App's assets folder.
You may have to create the folder.
If using Android SQLiteAssetHelper then you will need to create a databases folder in the assets folder.
There are various tools for Creating and Managing SQLite Databases. e.g. Db Browser for SQLite.
You then need to modify your App to copy the file from the assets folder (or assets/databases folder) and then open the database. Noting that you only do the copy if the database doesn't already exist.
Using the Android SQLiteAssetHelper simplifies this process.
I know about copying the database file solutoin,
How do I backup a database file to the SD card on Android?
Android backup/restore: how to backup an internal database?
Android backup/restore: how to backup an internal database?
in fact I was backing up my databases using this method, and up until a little while ago it was working perfectly.
until some multi-thread stuff made me use of enableWriteAheadLogging
now I have two more files near the database file with .db-wal and .db-shm extensions.
copying just the datbase file .db is not working as most of the times the file does not contain the latest database commits (which are available throw app itself) however when I copy three files together it seems to work fine (not quit sure, albeit)
as Sqlite people recommend, the best practice for backing up a sqlite database is to use backup api but can someone guid me on how I can use this api from inside an application, or even use sqlite .dump (How do I dump the data of some SQLite3 tables?) method from inside an app ?
So Which one is the best practice to back up a sqlite database from an android app?
1- Copy all the database-related files from the sandbox
2- Use sqlite Backup Api
3- Use sqlite .dump
4- any other method
The SQLite backup API is the only mechanism that works correctly with concurrent write accesses from other threads/processes. Which of course means that the Android database framework does not give you access to it.
If you are sure that there are not any active connections, you can just copy all files. (Leaving out journal or WAL files would lead to data corruption.) In the case of WAL, the -shm file
does not contain any permanent data and could be omitted.
The VACUUM INTO command introduced in SQLite version 3.27.0 (2019-02-07) can serve as an alternative to the backup API.
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'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
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.