This post is about "How to create Sqlite Database" and "How to store and retrieve value from SQlite DataBase "
Step - 1 You have to create DataBaseHanler Class .
DatabaseHandler.java
AndroidSQLiteTutorialActivity.java
You will find created Database from following steps-
Step 1- Open DDMS
Step 2- Open File Explorer
Step 3- Click on data
Step 4- Again Explore data
Step 5- Now find you package name
Step 6- Click on database
Step 7- Now you can see your database file's name
Step 8- Export this file in to your PC
Hope this code helps you!!!!
Happy Coding...!!!!:)
Step - 1 You have to create DataBaseHanler Class .
DatabaseHandler.java
package com.androidhive.androidsqlite; import java.util.ArrayList; import java.util.List; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; public class DatabaseHandler extends SQLiteOpenHelper { // All Static variables // Database Version private static final int DATABASE_VERSION = 1; // Database Name private static final String DATABASE_NAME = "MyDatabase"; // Database table name private static final String TABLE_LIST = "MyListItem"; // Table Columns names private static final String KEY_ID = "id"; private static final String KEY_ListItem = "listitem"; public DatabaseHandler(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } // Creating Tables @Override public void onCreate(SQLiteDatabase db) { String CREATE_LIST_TABLE = "CREATE TABLE " + TABLE_LIST + "(" + KEY_ID + " INTEGER," + KEY_ListItem + " TEXT" + ")"; db.execSQL(CREATE_LIST_TABLE); } // Upgrading database @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // Drop older table if existed db.execSQL("DROP TABLE IF EXISTS " + TABLE_LIST); // Create tables again onCreate(db); } void addListItem(ArrayList<String> listItem) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); for (int i = 0; i < listItem.size(); i++) { Log.e("vlaue inserting==", "" + listItem.get(i)); values.put(KEY_ListItem, listItem.get(i)); values.put(KEY_ID, i); db.insert(TABLE_LIST, null, values); } db.close(); // Closing database connection } Cursor getListItem() { String selectQuery = "SELECT * FROM " + TABLE_LIST; SQLiteDatabase db = this.getWritableDatabase(); Cursor cursor = db.rawQuery(selectQuery, null); return cursor; } }
AndroidSQLiteTutorialActivity.java
package com.androidhive.androidsqlite; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.database.Cursor; import android.os.Bundle; import android.util.Log; public class AndroidSQLiteTutorialActivity extends Activity { /** Called when the activity is first created. */ ArrayList<String> your_list_arrayArrayList; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); your_list_arrayArrayList = new ArrayList<String>(); your_list_arrayArrayList.add("Item1"); your_list_arrayArrayList.add("Item2"); your_list_arrayArrayList.add("Item3"); your_list_arrayArrayList.add("Item4"); your_list_arrayArrayList.add("Item5"); DatabaseHandler db = new DatabaseHandler(this); db.addListItem(your_list_arrayArrayList); Cursor cursor = db.getListItem(); Log.e("count", "" + cursor.getCount()); if (cursor != null) { cursor.moveToNext(); do { Log.e("value==", "" + cursor.getString(1)); Log.e("value==", "" + cursor.getString(0)); } while (cursor.moveToNext()); } } }
You will find created Database from following steps-
Step 1- Open DDMS
Step 2- Open File Explorer
Step 3- Click on data
Step 4- Again Explore data
Step 5- Now find you package name
Step 6- Click on database
Step 7- Now you can see your database file's name
Step 8- Export this file in to your PC
Hope this code helps you!!!!
Happy Coding...!!!!:)