How to use rawQuery in android

I have a database table with three columns: id, name, permission.

It looks like this:

1 comics fun

2 Communication conversation

3 comics to watch

I am trying to get permission where the name is comic. I use the following code in my database class (AppData.java):

private final static String DB_NAME = "safety_app_database"; // the name of our database private final static int DB_VERSION = 1; // the version of the database // the names for our database columns private final String TABLE_NAME = "permissions_table"; private final String ID = "id"; private final String NAME = "name"; private final String PERMISSION = "permission"; 

and method

 public Cursor getData(){ return db.rawQuery("SELECT permission FROM permissions_table WHERE name = 'Comics', null); } 

and I call it in my main class (security.java). AppData refers to AppData.java

 appData.getData(); 

Is this the right way to do this? I am not sure and this gives me an error when I try to invoke sql query.

+7
source share
4 answers

One typo error, you do not close the sql string with " . Try this.

 return db.rawQuery("SELECT permission FROM permissions_table WHERE name = 'Comics' ", null); 

[EDIT: JAR refers to Android 2.2, which does not allow you to modify the original attachments in your posts]

Jar of this blongs class file for an Android 2.0.1 container that does not allow modification

+4
source

There is no need for a raw request. You can use one of the recommended SQLiteDatabase query methods, and it will look like this:

 db.query("permissions_table", new String [] {permission}, "name = \'Comics\'", null, null, null, null); 
0
source

Try the following approach:

 public Cursor getData(String arg) { return db.rawQuery("SELECT permission FROM `permissions_table` WHERE name ="+arg, null); } 

Then just call the method described above with the correct parameter:

 Cursor cursor; cursor = getData ( "Comic" ); 
0
source

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

0
source

All Articles