Lorem ipsum dolor sit amet, consectetur adipiscing elit. Test link

android sqlite crud operations class

android sqlite crud operations class

SQLite CRUD (Create, Read, Update, Delete) operation class in Android for a table with fields "id" (autoincrement), "name", "address", and "mobile_no".

First, create a new class called "DatabaseHelper" that extends the SQLiteOpenHelper class:


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "mydatabase.db";
    private static final int DATABASE_VERSION = 1;

    // Table name
    private static final String TABLE_NAME = "mytable";

    // Column names
    private static final String COLUMN_ID = "id";
    private static final String COLUMN_NAME = "name";
    private static final String COLUMN_ADDRESS = "address";
    private static final String COLUMN_MOBILE_NO = "mobile_no";

    // SQL statement to create the table
    private static final String SQL_CREATE_TABLE =
            "CREATE TABLE " + TABLE_NAME + " (" +
                    COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
                    COLUMN_NAME + " TEXT," +
                    COLUMN_ADDRESS + " TEXT," +
                    COLUMN_MOBILE_NO + " TEXT)";

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(SQL_CREATE_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop table if exists and create new one
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }

    // CRUD operations

    public void addData(String name, String address, String mobileNo) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(COLUMN_NAME, name);
        values.put(COLUMN_ADDRESS, address);
        values.put(COLUMN_MOBILE_NO, mobileNo);

        db.insert(TABLE_NAME, null, values);
        db.close();
    }

    public Cursor getAllData() {
        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);

        return cursor;
    }

    public void updateData(int id, String name, String address, String mobileNo) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(COLUMN_NAME, name);
        values.put(COLUMN_ADDRESS, address);
        values.put(COLUMN_MOBILE_NO, mobileNo);

        db.update(TABLE_NAME, values, COLUMN_ID + " = ?", new String[] { String.valueOf(id) });
        db.close();
    }

    public void deleteData(int id) {
        SQLiteDatabase db = this.getWritableDatabase();

        db.delete(TABLE_NAME, COLUMN_ID + " = ?", new String[] { String.valueOf(id) });
        db.close();
    }

}

        

In this code, the DATABASE_NAME and DATABASE_VERSION constants define the name and version number of the database. The TABLE_NAME constant defines the name of the table, and the COLUMN_ID, COLUMN_NAME, COLUMN_ADDRESS, and COLUMN_MOBILE_NO constants define the names of the columns in the table.
The SQL_CREATE_TABLE constant contains the SQL statement to create the table.
The addData() method inserts a new row into the table with the given name, address, and mobile number.
The getAllData() method returns a Cursor object containing all the rows in the table.
The updateData() method updates a row in the table with the given ID, name, address, and mobile number.

Post a Comment