Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How To use dataBase in Your android App

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 413
    Comment on it

    Use DataBase Helper Class in your code, Which extends the base class SQLiteOpenHelper Class

    Sample code

    package com.searchoffers.android;

    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 com.searchoffers.android.data.VoucherDetailsData;

    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 = "VoucherDetailsDatasManager"; // VoucherDetailsDatas table name private static final String TABLE_VoucherDetailsDataS = "VoucherDetailsDatas"; // VoucherDetailsDatas Table Columns names private static final String KEY_ID = "id"; private static final String KEY_NAME = "name"; private static final String KEY_IMAGE_URL = "imageurl"; public DatabaseHandler(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); System.out.println("creating data base"); } // Creating Tables @Override public void onCreate(SQLiteDatabase db) { System.out.println("ion oncreate Data base"); String CREATE_VoucherDetailsDataS_TABLE = "CREATE TABLE " + TABLE_VoucherDetailsDataS + "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT," + KEY_IMAGE_URL +" TEXT" + ")"; db.execSQL(CREATE_VoucherDetailsDataS_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_VoucherDetailsDataS); // Create tables again onCreate(db); } /** * All CRUD(Create, Read, Update, Delete) Operations */ // Adding new VoucherDetailsData void addVoucherDetailsData(VoucherDetailsData VoucherDetailsData) { System.out.println("Seting name in database::" + VoucherDetailsData.getName()+VoucherDetailsData.getImageUrl()); SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(KEY_NAME, VoucherDetailsData.getName()); // VoucherDetailsData values.put(KEY_IMAGE_URL, VoucherDetailsData.getImageUrl()); // Name db.insert(TABLE_VoucherDetailsDataS, null, values); db.close(); // Closing database connection } // Getting All VoucherDetailsDatas public List<VoucherDetailsData> getAllVoucherDetailsDatas() { List<VoucherDetailsData> VoucherDetailsDataList = new ArrayList<VoucherDetailsData>(); System.out.println("In voucher detail get data"); // Select All Query String selectQuery = "SELECT * FROM " + TABLE_VoucherDetailsDataS; SQLiteDatabase db = this.getWritableDatabase(); Cursor cursor = db.rawQuery(selectQuery, null); // looping through all rows and adding to list if (cursor.moveToFirst()) { do { VoucherDetailsData voucherDetailsData = new VoucherDetailsData(); voucherDetailsData.setId(Integer.parseInt(cursor.getString(0))); voucherDetailsData.setName(cursor.getString(1)); voucherDetailsData.setImageUrl(cursor.getString(2)); // Adding VoucherDetailsData to list VoucherDetailsDataList.add(voucherDetailsData); System.out.println("Geting from database id::" + cursor.getString(0)); System.out.println("Geting from database name::" + cursor.getString(1)); System.out.println("Geting from database imageurl::" + cursor.getString(2)); } while (cursor.moveToNext()); } return VoucherDetailsDataList; } // Getting VoucherDetailsDatas Count public int getVoucherDetailsDatasCount() { String countQuery = "SELECT * FROM " + TABLE_VoucherDetailsDataS; SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.rawQuery(countQuery, null); int count=cursor.getCount(); cursor.close(); return count; }

    }

    you can use this link for further refrence http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: