My requirement is to create a db helper class for android which will do all data base operations. but by passing a class to the db helper, it should create a table with the field names as columns.
In that way i should be able to use this for any class with any fields.
Post any of the links related to this or any hints for achieving this.
Response is appreciated.
Related
Curtently, my app loads some data from rest webservice, using spring's RestTemplate and java pojo classes with Jackson.
So as a result, I have java objects, containing data retrieved from webservice.
But I also need to have my data available for offline use. So my idea is to save data to sqlite database. Can I use my pojo classes as a models for ormlite to save/load data?
I want to directly save my data to sqlite, using pojo class objects that I already have as ormlite models, is it possible with ormlite? Note that my pojo classes contain fields declared as other pojo object, so probably there will be needed some table structure with foreign keys. Can it be handled automatically by ormlite?
You have to annotate your POJO Model with proper ormlite annotations #DatabaseTable and #DatabaseColumn.
Nextly you have to build up a class(eg. OrmDatabaseHelper) that extends OrmLiteSqliteOpenHelper;
Afterwards you can inject object like this
#OrmLiteDao(helper = OrmDatabaseHelper.class)
Dao<YourCustomPOJO, Long> yourCustomDao;
Long stands for the ID field type.
This allows you to use dao's methods like create, delete, queryForAll etc.
YourCustomPOJO pojo = new YourCustomPOJO();
//set some values
yourCustomDao.create(pojo);
I was watching android tutorial on databases. They create a class and extend SQLiteOpenHelper. Then they would create a static final string for the database name. When you request a writable database it will always select using the static string. What is the convention used for selecting a different database? Just create another class and extend SQLiteOpenHelper? Also can you pass SQLiteDatabase from one activity to another using intent.putExtra or similar method?
What is the convention used for selecting a different database?
The SQLiteOpenHelper handles more than just a database name. It handles schema version, creation, upgrades, etc... So I would expect the convention to be to define another class and extend SQLiteOpenHelper.
Can you pass SQLiteDatabase from one activity to another using intent.putExtra or similar method?
No, you can't. But you can create a new object of the class describing the database (that one that extends SQLiteOpenHelper, and request a database from it. Internally the SQLiteOpenHelper keeps a single writable connection to a database.
How can i open a database in a class that is not a subclass of Activity?
In a subclass of Activity, i can use openOrCreateDatabase() but can i open a database in a different class?
I tried making the database instance a static one and open it in an Activity and get the static instance in the other class, but it throws an exception stating the database is closed.
Check out this tutorial.
I went through it and it's a really good tutorial on how to use SQLite in Android.
Essentially you need to create a database helper class that will do the table creation. Then you can use this helper class in your Activity to create your database and or tables.
It's common practice to use a SQLite Database Adapter and sometimes a helper class that is separate from the activity that is utilizing the database. Here is a link to a example that uses the code. THe vogella tutorial is also good but the use of a ContentProvider makes it a bit tough to understand what things need to be in there for a SQLite DB only.
Essentially, the helper class is responsible for creating, updating and deleting the DB while the adapter class handles the methods for changing values, deleting rows, and actually calling the helper to open the database.
Is it possible to use some kind of ddl script in SQLiteHelper to create database?
Yes. I usually have a main DB class in my application that encapsulates an inner class that derives from SQLiteOpenHelper. In the onCreate, you get a SQLiteDatabase instance as a parameter. Call db.execSQL(String) with the appropriate script(s) to create your tables and populate them.
I have a singleton class that creates a connection to a Sqlite db and runs queries. I need to pull the database stuff out of the Singleton and create a database handler class.
My question is: Does the database handler class also need to be a Singleton?
Thanks.
Probably not, you could have a class that is instantiated normally every time and used like every other class, I do not write singleton data layers since long time and not even static classes for it any more.
Actually you don't need to write your own singleton.
You just need to have a class which inherits from SQLiteOpenHelper
Later in the code you just need to use: SQLiteDatabase db = helper.getWritableDatabase();
The SQLiteOpenHelper cares if the D already exists. If yes, it gives to the DB as readable or writeable database.
If no DB is available it creates one... like this you don't have to check if the DB is already created.
You can see here a full example i posted some days ago...
Android - Sqlite database method undefined fot type
Hope this helps :)