Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would suggest you to refer SQLiteOpenHelper instead of hard-coding queries.</p> <p>Following code will help you</p> <p><strong>Employee Model Class</strong> </p> <pre><code>package com.database; public class Employee { int _id; String _name; String _phone_number; public Employee (){ } // constructor public Employee (int id, String name, String _phone_number){ this._id = id; this._name = name; this._phone_number = _phone_number; } // constructor public Employee (String name, String _phone_number){ this._name = name; this._phone_number = _phone_number; } // getting ID public int getID(){ return this._id; } // setting id public void setID(int id){ this._id = id; } // getting name public String getName(){ return this._name; } // setting name public void setName(String name){ this._name = name; } // getting phone number public String getPhoneNumber(){ return this._phone_number; } // setting phone number public void setPhoneNumber(String phone_number){ this._phone_number = phone_number; } } </code></pre> <p><strong>SQLite Helper Class</strong></p> <p>public class DatabaseHelper extends SQLiteOpenHelper {</p> <pre><code> private static final int DATABASE_VERSION = 1; private static final String DATABASE_NAME = "employeeDB"; private static final String TABLE_EMPLOYEE = "employee"; private static final String KEY_ID = "id"; private static final String KEY_NAME = "name"; private static final String KEY_PH_NO = "phone_number"; public DatabaseHelper (Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } // Creating Tables @Override public void onCreate(SQLiteDatabase db) { String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_EMPLOYEE + "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT," + KEY_PH_NO + " TEXT" + ")"; db.execSQL(CREATE_CONTACTS_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_CONTACTS); // Create tables again onCreate(db); } </code></pre> <p><strong>CRUD Operations</strong></p> <p>// <strong>Adding new Employee</strong></p> <pre><code>public void addEmployee(Employee employee) {} </code></pre> <p>// <strong>Getting single Employee</strong></p> <pre><code>public Contact getEmployee(int id) {} </code></pre> <p>// <strong>Getting All Employees</strong></p> <pre><code>public List&lt;Employee&gt; getAllEmployees() {} </code></pre> <p>// <strong>Getting Employees Count</strong></p> <pre><code>public int getEmployeesCount() {} </code></pre> <p>// <strong>Updating single Employee</strong></p> <pre><code>public int updateEmployee(Employee employee) {} </code></pre> <p>// <strong>Deleting single Employee</strong></p> <pre><code>public void deleteEmployee(Employee employee) {} </code></pre> <p><strong>Inserting new Record</strong></p> <pre><code>public void addemployee(Employee employee) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(KEY_NAME, employee.getName()); values.put(KEY_PH_NO, employee.getPhoneNumber()); db.insert(TABLE_EMPLOYEE, null, values); db.close(); } </code></pre> <p><strong>Reading Row(s)</strong></p> <pre><code>public Contact getEmployee(int id) { SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.query(TABLE_EMPLOYEE, new String[] { KEY_ID, KEY_NAME, KEY_PH_NO }, KEY_ID + "=?", new String[] { String.valueOf(id) }, null, null, null, null); if (cursor != null) cursor.moveToFirst(); Employee employee= new Employee(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2)); return employee; } </code></pre> <p><strong>Getting All Employees</strong></p> <pre><code>public List&lt;Contact&gt; getAllEmployees() { List&lt;Employee&gt; employeeList = new ArrayList&lt;Employee&gt;(); // Select All Query String selectQuery = "SELECT * FROM " + TABLE_EMPLOYEE; SQLiteDatabase db = this.getWritableDatabase(); Cursor cursor = db.rawQuery(selectQuery, null); // looping through all rows and adding to list if (cursor.moveToFirst()) { do { Employee employee= new Employee(); employee.setID(Integer.parseInt(cursor.getString(0))); employee.setName(cursor.getString(1)); employee.setPhoneNumber(cursor.getString(2)); employeeList.add(employee); } while (cursor.moveToNext()); } return employeeList; } </code></pre> <p><strong>Get All Employees Count</strong></p> <pre><code> public int getEmployeesCount() { String countQuery = "SELECT * FROM " + TABLE_EMPLOYEE; SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.rawQuery(countQuery, null); cursor.close(); // return count return cursor.getCount(); } </code></pre> <p><strong>Updating Record</strong></p> <pre><code>public int updateEmployee(Employee employee) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(KEY_NAME, employee.getName()); values.put(KEY_PH_NO, employee.getPhoneNumber()); // updating row return db.update(TABLE_EMPLOYEE, values, KEY_ID + " = ?", new String[] { String.valueOf(employee.getID()) }); } </code></pre> <p><strong>Deleting Record</strong></p> <pre><code>public void deleteEmployee(Employee employee) { SQLiteDatabase db = this.getWritableDatabase(); db.delete(TABLE_EMPLOYEE, KEY_ID + " = ?", new String[] { String.valueOf(employee.getID()) }); db.close(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload