可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I have a database table with 3 columns: id, name, permission.
It looks like this:
1 Comics fun
2 Communication talk
3 Comics watch
I am trying to get the permission where the name is comics. I am using 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 the method
public Cursor getData(){ return db.rawQuery("SELECT permission FROM permissions_table WHERE name = 'Comics', null); }
and I'm calling this in my main class (safety.java). AppData is referencing AppData.java
appData.getData();
Is this the correct way to do it? I am unsure, and it is giving me an error whenever I try to call the sql query.
回答1:
One typo mistake, you are not closing sql string with "
. try this.
return db.rawQuery("SELECT permission FROM permissions_table WHERE name = 'Comics' ", null);
[EDIT: JAR belongs to 'Android 2.2' which does not allow modifications to source attachments on its entries]
The Jar of this class file blongs to container Android 2.0.1 which does not allow modifications
回答2:
There is no need for a raw query. You can use one of the recommended SQLiteDatabase query methods and it would look like this:
db.query("permissions_table", new String [] {permission}, "name = \'Comics\'", null, null, null, null);
回答3:
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 above method with the right parameter like this:
Cursor cursor; cursor = getData ( "Comic" );
回答4:
To execute queries, there are two methods: Execute db.rawQuery method Execute db.query method To execute 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: The select statement String[] selection args: The arguments if a WHERE clause is included in the select statement Notes 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 form SELECT [Column Name] as _id cause the Cursor object always expects that the primary key column has the name _id or it will throw an exception . Another way to perform a query is to use a 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 against String [ ] columns: The projection of the query, i.e., the columns to retrieve String WHERE clause: where clause, if none pass null String [ ] selection args: The parameters of the WHERE clause String Group by: A string specifying group by clause String Having: A string specifying HAVING clause String Order By by: A string Order By by clause