Android SQLiteOpenHelper existing database - android

Newbie Q coming from iPhone/MonoTouch C# background
I have an existing SQLite database file from another project.
Where do I include the database file into my Eclipse project to have it deploy with the app.
Do I need to indicate that the database file is writable? (In the iPhone world you need to copy the database file from the app's bundle to a writable folder on the iPhone proper before first use.)
Once I have the database file on the phone, how do I tell SQLiteOpenHelper to use it? (I extend SQLiteOpenHelper in a custom class.

Where do I include the database file into my Eclipse project to have it deploy with the app.
You can place the db file in either res/raw or assets/.
Do I need to indicate that the database file is writable? (In the iPhone world you need to
copy the database file from the app's bundle to a writable folder on the iPhone proper before first use.)
With Android, you'll have to copy the file from your bundle into the app's database directory. Then, your app should be able to open the database using SQLiteOpenHelper as you would any other database created by your app.
To open the file (assuming you placed it in res/raw), call Resource.openRawResource(id) and to get the destination directory, call Context.getDatabasePath(filename). The database path might not exist, so you might need to create the parent directories first.

Related

how to connect sqlite database with an android project

I have created an SQLite database by using DB Browser for SQLite.
I want to connect this database with my android project ..
So, where should I put it ? in which folder of my project ?
and how can I connect them together ? just with SQLiteOpenHelper ?
So, where should I put it ? in which folder of my project ?
Typically you'd put the database file into the assets folder or within a folder within the assets folder.
how can I connect them together ? just with SQLiteOpenHelper ?
You would typically then connect to the database after copying the database from the assets folder to a suitable location (easiest place is in data/data//databases).
Although you could do the above via a subclass of SQLiteOpenHelper, there is a simpler way by using Android SQLiteAssetHelper. Instructions are in the README.
Note the database file needs to be copied into the assets/databases folder as that is where SQLiteAssethelper expects the file to be.

Backup SQLite database and restore it back when the Android application is installed

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.

Cordova Android: Open a SQLite .db database from an arbitrary directory

I have a Cordova mobile app and I let the user import a SQLite dababase from an external .db file. I'm using the cordova-sqlite-storage
https://github.com/litehelpers/Cordova-sqlite-storage
A naive solution is already implemented but it's risky. Basically the user selects an external .db file using a file picker which replaces the old .db file
window.plugins.sqlDB.copyDbFromStorage("mydb.db", 0, file, true, onSuccess);
where file is the uri of the new .db file.
It works but I'd like to check the .db file first, at least those two checks:
check if the .db file is actually a SQLite database
check if the new SQLite database has some table that must be there
before importing
What I've tried
I tried to find a way to open the .db file from its original directory but it seems that the openDatabase() only opens databases stored in the special default location (which is, I guess, the internal private storage for the app)
openDatabase({name: 'test.db', location: 'default'});
other values for the location parameter are simply discouraged, they suggest to stick with default
If you cannot (or do not want to) open the database at the original location, then you have to copy it to the internal private storage location with a different file name. (You can then later copy over the old file, or delete and rename the files.)

how to save static database android app on separate folder

I have created a database android app for my college that stores students records and all the record are store in sqlitedatabse and the file name is STUDENT.sqlite.But that file is hidden and can be seen only if phone is rooted.so i need a solution so that the aap gets install it should show separate folder for it and the sqlite file mustbe there in it.so that when i put all records in that and save i can give that file so another person and he can view the same record.no need of entering all records again he can just change the sqlite file.
Maybe you can copy all records from your database into a public or shared directory on sd-card?
Rename the database file to yourdatabase.db and put it in your projects assets folder and on first run of application just copy that database to data directory of android system.

Accessing SQLite Database Through Assets

Im trying to use ionic with the SQLite plugin (https://github.com/brodysoft/Cordova-SQLitePlugin) and I was able to create and use a database, but in my app i need a prepopulated database.
I see methods of achieving this by placing the database in the platforms/android/assets folder in the ionic project, and then copying it, on the first android run, to the "correct location".
My question is, why is it needed to copy it to another location? why cant I just access it from the assets folder which the application creates? If it was an image, i wouldnt need to change its place either, i would use it from the assets, so why not the db too?
Your database file in the assets folder is stored in an exported file format (minimum storage space required, but not for interactive use). The Android SQLite Database needs to import this file. This means reading and storing it in a new format for better searching/reading and writing.
So it is not a simple copying process it's an interaction and after the import your database does not read from your assets folder any longer. So replacing your database in your assets folder does not update your imported database.
You can use this project to import your database in android: https://github.com/jgilfelt/android-sqlite-asset-helper

Categories

Resources