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.
Related
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.
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 necessary to extends SQLiteOpenHelper.I just want to copy the database(read only) from aseets folder.
It is not necessary to extends SQLiteOpenHelper,To create and upgrade a database in your Android application you usually subclass "SQLiteOpenHelper". In this class you need to override the methods onCreate() to create the database and onUpgrade() to upgrade the database in case of changes in the database schema. Both methods receive an "SQLiteDatabase" object.
SQLiteOpenHelper provides the methods getReadableDatabase() and getWriteableDatabase() to get access to an "SQLiteDatabase" object which allows database access either in read or write mode.
For the primary key of the database you should always use the identifier "_id" as some of Android functions rely on this standard.
if you just want to copy the database then its fine.
But if you want to read,update,delete or create new record in database then there are two ways
Extend sqlitedatabase
Use ormlite wrapper
Hope this will help.
Yes, it not need ,if you just want copy database file and don`t operate database, it just a file I/O operate.
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 :)