Executing Queries
To execute queries, there are two methods:- Execute
db.rawQuery
method - Execute
db.query
method
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
: Theselect
statementString[] selection args
: The arguments if aWHERE
clause is included in theselect
statement
- The result of a query is returned in
Cursor
object. - In
a
select
statement 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_id
cause theCursor
object always expects that the primary key column has the name_id
or 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 WHERE
clause:where
clause, if none passnull
String [ ]
selection args: The parameters of theWHERE
clauseString Group by
: Astring
specifying group by clauseString Having
: Astring
specifyingHAVING
clauseString Order By by
: Astring Order By by
clause
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