Android Sqlite Database Tutorial
1.Getter and Setter of contact class:
package com.example.gangsofcoder.testdemo.SqliteDemo; public class Contact { int contactId; String contactName, contactPhoneNumber; public Contact() { } public Contact(int id, String name, String number) { contactId = id; contactPhoneNumber = name; contactName = number; } public Contact(String name, String number) { contactName = name; contactPhoneNumber = number; } public int getContactId() { return contactId; } public void setContactId(int contactId) { this.contactId = contactId; } public String getContactName() { return contactName; } public void setContactName(String contactName) { this.contactName = contactName; } public String getContactPhoneNumber() { return contactPhoneNumber; } public void setContactPhoneNumber(String contactPhoneNumber) { this.contactPhoneNumber = contactPhoneNumber; } }
2.Database Handler Class for CRUD Operation
package com.example.gangsofcoder.testdemo.SqliteDemo; 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; import java.util.ArrayList; import java.util.List; public class DatabaseHandler extends SQLiteOpenHelper { private static final int DATABASE_VERSION = 1; private static final String DATABASE_NAME = "contactDb"; private static final String TABLE_CONTACT = "contactTable"; //column name private static final String KEY_ID = "id"; private static final String KEY_NAME = "name"; private static final String KEY_NUMBER = "number"; public DatabaseHandler(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { String CONTACT_TABLE = "CREATE TABLE " + TABLE_CONTACT + "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT," + KEY_NUMBER + " TEXT" + ")"; Log.d("data=", "inside onCreate"); Log.d("data=", CONTACT_TABLE); db.execSQL(CONTACT_TABLE); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXIST " + TABLE_CONTACT); onCreate(db); } // Adding new contact public void addContact(Contact contact) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(KEY_NAME, contact.getContactName()); values.put(KEY_NUMBER, contact.getContactPhoneNumber()); db.insert(TABLE_CONTACT, null, values); db.close(); } // Getting single contact public Contact getContact(int id) { SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.query(TABLE_CONTACT, new String[]{KEY_ID, KEY_NAME, KEY_NUMBER}, KEY_ID + "=?", new String[]{String.valueOf(id)}, null, null, null, null); if (cursor != null) { cursor.moveToFirst(); } Contact contact = new Contact(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2)); return contact; } // Getting All Contacts public List<Contact> getAllContacts() { List<Contact> contactList = new ArrayList<>(); String SELECT_QUERY = "SELECT * FROM " + TABLE_CONTACT; SQLiteDatabase db = this.getWritableDatabase(); Cursor cursor = db.rawQuery(SELECT_QUERY, null); Log.d("data=", String.valueOf(cursor)); if (cursor.moveToFirst()) { do { Contact contact = new Contact(); contact.setContactId(Integer.parseInt(cursor.getString(0))); contact.setContactName(cursor.getString(1)); contact.setContactPhoneNumber(cursor.getString(2)); Log.d("data=", cursor.getString(1)); contactList.add(contact); } while (cursor.moveToNext()); } return contactList; } // Getting contacts Count public int getContactsCount() { String SELECT_QUERY = "SELECT * FROM " + TABLE_CONTACT; SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.rawQuery(SELECT_QUERY, null); cursor.close(); return cursor.getCount(); } // Updating single contact public int updateContact(Contact contact) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(KEY_NAME, contact.getContactName()); values.put(KEY_NUMBER, contact.getContactPhoneNumber()); return db.update(TABLE_CONTACT, values, KEY_ID + "=?", new String[]{String.valueOf(contact.contactId)}); } // Deleting single contact public void deleteContact(Contact contact) { SQLiteDatabase db = this.getWritableDatabase(); db.delete(TABLE_CONTACT, KEY_ID + "= ?", new String[]{String.valueOf(contact.getContactId())}); db.close(); } }
3.Class from where we are calling other two class
package com.example.gangsofcoder.testdemo.SqliteDemo; import android.content.ContentValues; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.widget.TextView; import com.example.gangsofcoder.testdemo.R; import org.w3c.dom.Text; import java.util.List; public class SqliteActivity extends AppCompatActivity { TextView tvResult; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.sqlite_activity); tvResult = (TextView) findViewById(R.id.tvResult); DatabaseHandler db = new DatabaseHandler(this); db.addContact(new Contact("A", "7355173572")); db.addContact(new Contact("B", "8699696625")); db.addContact(new Contact("C", "1123456854")); db.addContact(new Contact("D", "1418529635")); db.addContact(new Contact("E", "1234567897")); List<Contact> contactList = db.getAllContacts(); for (Contact cn : contactList) { String log = "Id: " + cn.getContactId() + " ,Name: " + cn.getContactName() + " ,Phone: " + cn.getContactPhoneNumber(); tvResult.setText(log); } } }
No comments :
Post a Comment