Hi, storing data of an application is one of the basic needs, so as you may know android provides several options to store data internally and externally. Such as shared preferences, SQLite databases, Content Providers, external storage and etc. You can check it at the android developers site ->http://developer.android.com/guide/topics/data/data-storage.html .
To create an internal database you can use either SQLite or Content Providers. In case of choosing the proper method is depend on the usability of the data you store. As an example if your application store contact details and those data need to be accessed via another applications you can use Content providers to create the particular private database. Otherwise can create a SQLite database which allows you to store and use data entirely within your own application.
In my previous posts I’ve discussed how to use Eclipse ‘File Explorer’ view to export android internal databases and the use of ‘SQLite Database Browser’. They will be useful when creating internal databases for your android applications. Here I’ll give you a step by step guide to create a SQLite internal database. Hope you’ll enjoy reading the post. 🙂
step 1 :
Create a new project using either android API or the Google API. In addition to the Activity class add another class called “DBAdapter” to the same package. Inside this class,
- Define the column names of the database.
public static final String INDEX =”_index”;
public static final String STU_ID =”_id”;
public static final String STU_NAME =”name”;
- Define the database name and the table name.
private static final String DATABASE_NAME = “studentdata.db”;
private static final String DATABASE_TABLE = “studentDetails”;
- Constructor :
public DBAdapter(Context ctx){
this.context=ctx;
dbHelper =new DBHelper(context);
Log.d(“DBAdapter: “, “DBAdapter Creating ..”); }
- Add the db open and close methods
private SQLiteDatabase db;
public DBAdapter open(){
db=dbHelper.getWritableDatabase();
return this; }
// close the database
public void close(){
dbHelper.close();
}
step 2 :
Inside the DBAdapter class write another private class which is a subclass of ‘SQLiteOpenHelper’. This helper class uses to manage database creation and version management.
private static String DATABASE_CREATE;
private static final int DATABASE_VERSION = 1;
private class DBHelper extends SQLiteOpenHelper{
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
Log.d(“DBHelper :”,”DBHelper Creating ..”); }
@Override public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
Log.d(“DB: “, “DB Creating ..”);
DATABASE_CREATE= “create table “+DATABASE_TABLE+”(“+ INDEX+” integer primary key autoincrement,” + STU_ID+” text not null,” + STU_NAME+” text not null);”;
db.execSQL(DATABASE_CREATE); }
@Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
} }
step 3 :
Now create an instance of DBAdapter inside the Activity class and call the open(). Then the SQLite database will be created on your device.
public class AndroidDBActivity extends Activity { /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DBAdapter dba=new DBAdapter(this);
//open the database
dba.open();
}}
Deploying..
When you deploy the application on the Emulator, you can determine that the database is created using ‘LogCat’. If the application runs without any Runtime Exceptions you will get the following output.
Check with SQLite Database Brower
step 1 :
Export the db file using ‘File Explorer view’
Use the Guide :-> https://techstarspace.wordpress.com/2012/08/09/export-android-internal-databases/
step 2 :
Now view the database structure using ‘SQLite Database Browser’
Use the Guide :->https://techstarspace.wordpress.com/2012/08/16/sqlite-database-browser/ Thank You! 🙂