Code example for Cursor

Methods: getColumnIndexgetStringmoveToFirst

0
        String name = "nullone";
 
        database.execSQL("insert into exectable (first_column, name) values (?,?);", new String[]{null, name});
 
        Cursor cursor = database.rawQuery("select * from exectable WHERE `name` = ?", new String[]{name});
        cursor.moveToFirst();
        int firstIndex = cursor.getColumnIndex("first_column");
        int nameIndex = cursor.getColumnIndex("name");
        assertThat(cursor.getString(nameIndex)).isEqualTo(name);
        assertThat(cursor.getString(firstIndex)).isEqualTo((String) null);
 
    } 
 
    @Test(expected = Exception.class) 
    public void testExecSQLInsertNullShouldBeException() throws Exception { 
        //this inserts null in android, but it when it happens it is likely an error.  H2 throws an exception.  So we'll make Robolectric expect an Exception so that the error can be found. 
 
        database.delete("exectable", null, null);
 
        Cursor cursor = database.rawQuery("select * from exectable", null);