how to connect sqlite database with an android project - android

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.

Related

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.

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

Should I explicitly create SQLite tables in my app?

I am a novice, and am creating a simple app that just reads data from a table in an SQLite database and displays on the GUI. (Just select operation )
During development, I created the database and the table from the tool "SQLite Database browser". And I inserted all data into the table through the tool.
Now, my doubt is ..
1) In java code, should I have some method that creates database and table ?? (For now, I have methods to do the select operation alone)
2) The database I created is located in my local drive. When the apk file gets created would the database also be included in the apk file ??
Pls help !! Thanks in advance !!
1) In Java code, should I have some method that creates database and table ?? (For now, I have methods to do the select operation alone)
No, You have to just copy your database file from /asset directory to /data/data/<package_name>/database directory. And only use select operation alone. You don't need to use create Database and table operation..
2) The database I created is located in my local drive. When the apk file gets created would the database also be included in the apk file?
For this, as I mentioned above you have to first put your database file into application's /asset directory, then copy it to internal storage (when your application start), then it works.
Look at this SO question How to ship an Android application with a database?. It answered what you needed.
try below link
http://www.higherpass.com/Android/Tutorials/Accessing-Data-With-Android-Cursors/

How to access sqlite database which already exists in android

I have created an sqlite databse and put it into the assets folder. How can I access it? I have created a database helper class also, but my data is "not taking the path".
Plz help me
Thanks
Copy your database file from /asset to internal databases directory /data/data/<package_name>/databases/, then use that database file.
Look at this SO question How to ship an Android application with a database?.
Also this tutorial Using your own SQLite database in Android applications

Android SQLiteOpenHelper existing database

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.

Categories

Resources