Executing Queries
To execute queries, there are two methods:- Execute
db.rawQuerymethod - Execute
db.querymethod
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: TheselectstatementString[] selection args: The arguments if aWHEREclause is included in theselectstatement
- The result of a query is returned in
Cursorobject. - In
a
selectstatement if the primary key column (the id column) of the table has a name other than_id, then you have to use an alias in the formSELECT[Column Name] as_idcause theCursorobject always expects that the primary key column has the name_idor it will throw an exception .
db.query method.
A query to select all employees in a certain department from a view
would be like this: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;
}
The
db.query has
the following parameters:String Table Name: The name of the table to run the query againstString [ ]columns: The projection of the query, i.e., the columns to retrieveString WHEREclause:whereclause, if none passnullString [ ]selection args: The parameters of theWHEREclauseString Group by: Astringspecifying group by clauseString Having: AstringspecifyingHAVINGclauseString Order By by: Astring Order By byclause
Thanks for this posting
ReplyDeleteYour choice of colors and font colors makes this site regretable
ReplyDeleteCompletely agree with you.
DeleteAgreed, what kind of terrible colors you have selected. Made my eyes blur!
DeleteBro, change the colors of the font, man. I can see nothing clearly. Good content but bad color choice.
ReplyDeletewhite font colour on white background colour.....ARE YOU DUMB FOOL....HOW CAN WE READ THIS POST IDIOT
ReplyDelete