Search code examples
androidandroid-sqlite

what is the right way to write this simple sqlite rawquery?


I am using sqlite after a lot of years.
I am writing a simple query in sqlite but getting error.
I searched google, but they are showing advanced errors, none shows this simple error.
code:

**public Cursor getLastData(int data) {
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor res = db.rawQuery("select * from "+TABLE_NAME, selectionArgs:"STATUS = 1");// error here near status = 1
        return res;
    }**

Also can I use SQLiteDatabase db = this.getReadableDatabase();


Solution

  • I think it will work

    public Cursor getLastData(int data) {
                SQLiteDatabase sqLiteDatabase = this.getReadableDatabase();
        
                String query = "select * from "+ TABLE_NAME +" WHERE STATUS = 1";
        
                Cursor cursor = null ;
        
                try{
                    cursor = sqLiteDatabase.rawQuery(query,null);
                }catch (SQLException ex)
                {
                    Log.d("error",ex.toString());
                }
                return cursor ;
            }