What must I do to implement SQLite's rawQuery(

2019-09-08 07:36发布

问题:

I'm trying to use the SQLite rawQuery method, based on the code here:

Android record exists() in database?

...but I get "The method rawQuery(String, String[]) is undefined for the type OdaaDBOpenHelper Authorize_Activity_DynamicControls.java" with this code:

    OdaaDBOH = new OdaaDBOpenHelper(this);
. . .
    private boolean RecordExists(String _id) {
        // This:
        Cursor cursor = OdaaDBOH.rawQuery("select 1 from NAPOLEON_DYNAMITE_TABLE where _id=%s",  
        // or this:
        //Cursor cursor = OdaaDOdaaDBOpenHelperBOH.rawQuery("select 1 from     
NAPOLEON_DYNAMITE_TABLE where _id=%s",  
                new String[] { _id });
        boolean exists = (cursor.getCount() > 0); 
        cursor.close(); 
        return exists; 
    }

. . .
// from referenced unit:

public class OdaaDBOpenHelper extends SQLiteOpenHelper {

I tried it both using the instance name of my SQLiteOpenHelper-derived class, and using the class name itself (commented out in the code above).

What must I do to implement rawQuery() or cause it to be recognized/acknowledged?

回答1:

From that question correct answer:

Consider that mDb is your SqlLiteDatabase class...

OdaaDBOH should be an instance of a SQLiteDatabase if you want to use rawQuery on it, most likely you are providing the implementation of a SQLiteOpenHelper there, which of course doesn't have a method rawQuery.



回答2:

rawQuery() is a method on SQLiteDatabase, not SQLiteOpenHelper. Call getReadableDatabase() or getWritableDatabase() on your SQLiteOpenHelper to get a SQLiteDatabase.