7

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.

Carl Manaster
  • 39,912
  • 17
  • 102
  • 155
Hip Hip Array
  • 4,665
  • 11
  • 49
  • 80

4 Answers4

4

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

Community
  • 1
  • 1
Priyank
  • 10,503
  • 2
  • 27
  • 25
  • I believe you'll need to escape the single quotes here with \' – Maximus Apr 18 '11 at 18:51
  • no, it seems that it is not my sql, i tried using the following code and it still gave me an error "public Cursor getData(){ return db.rawQuery("SELECT * FROM permissions_table", null); " } – Hip Hip Array Apr 18 '11 at 19:01
  • Also, in the raw queries, or using the execSQL method you must end the string with a semicolon. – Maximus Apr 18 '11 at 19:05
  • @Niall: put up exact error that you receive. that would help us. – Priyank Apr 18 '11 at 19:08
  • ok, when i run it through the debugger it gives a invocationTargetException error. it says the JAR belongs to 'Android 2.2' which does not allow modifications to source attachments on its entries – Hip Hip Array Apr 18 '11 at 19:13
  • This may give you a direction: http://stackoverflow.com/questions/2064100/the-jar-of-this-class-file-blongs-to-container-android-2-0-1-which-does-not-allow – Priyank Apr 18 '11 at 19:22
0

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" );
t0mm13b
  • 34,087
  • 8
  • 78
  • 110
Fred Ondieki
  • 2,314
  • 25
  • 23
0

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

0

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);
Maximus
  • 8,351
  • 3
  • 29
  • 37
  • 1
    no that didnt work either, just the same error when i try to run it and debug it. – Hip Hip Array Apr 18 '11 at 19:09
  • sorry it says : java.lang.NullPointerException at AppData.getData(AppData.java:71) – Hip Hip Array Apr 18 '11 at 19:16
  • Are you sure that it's this database query that's causing the error? – Maximus Apr 18 '11 at 19:18
  • just edited what it said and ya it is because everything is working perfectly when i take out the "appData.getData(...);" line – Hip Hip Array Apr 18 '11 at 19:21
  • Okay, if you're getting NullPointerException, then the Object appData was never instantiated. If you're calling this in AppData, why not call it with this.getData()? Or I suppose somewhere, like in onCreate() you would have to have appData = AppData.this; and a global declaration of AppData appData; – Maximus Apr 18 '11 at 19:30
  • i want to call getData from safety.java, and i have both the appData = AppData(this); and it declared globally... public class safety extends Activity { AppData appData; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); appData = new AppData(this); – Hip Hip Array Apr 18 '11 at 19:38
  • I apologize, I should have seen that from the question. I'm not sure if this kind of approach to running another Activity's methods is particularly advisable in the Android Framework. There is no instance that I'm aware of, that you should be calling an Activity's constructor like that, and it looks like an additional Constructor that you must have set up. And you're passing it the saftey Activity... I'm rather confused. I think you should reconsider your approach to executing that database query. – Maximus Apr 18 '11 at 20:00