There are two methods for executing queries: Run the db.rawQuery method Run the db.query method To run a raw query to retrieve all departments:
Cursor getAllDepts() { SQLiteDatabase db=this.getReadableDatabase(); Cursor cur=db.rawQuery("SELECT "+colDeptID+" as _id, "+colDeptName+" from "+deptTable,new String [] {}); return cur; }
The rawQuery method has two parameters: String query: select statement String [] selection args: arguments if the WHERE clause is included in the select Notes statement. The query result is returned in the Cursor object. In the select expression, if the primary key column (id column) of the table has a name other than _id, then you should use an alias in the form of SELECT [Column Name] as _id, because the Cursor object always expects the primary key column to be named _id or it throws an exception. Another way to execute a query is to use the db.query method. The request to select all employees in a certain department from the presentation will be as follows:
public Cursor getEmpByDept(String Dept) { SQLiteDatabase db=this.getReadableDatabase(); String [] columns=new String[]{"_id",colName,colAge,colDeptName}; Cursor c=db.query(viewEmps, columns, colDeptName+"=?", new String[]{Dept}, null, null, null); return c; }
Db.query has the following parameters: String Table Name: Name of the table to start the query on columns String []: Projecting the query, i.e. columns for extracting a sentence String WHERE: where where, if none passed null String [] selection args: Parameters of the WHERE clause String Group by: Group defining the string by sentence String After: String defining the sentence HAVING String Order By by: String Order By by clause
Savan Kachhiya Patel
source share