how to use rawQuery in android
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.
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
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);
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" );
链接地址: http://www.djcxy.com/p/52084.html
上一篇: 在Eclipse中刷新PyDev导入路径