任何人都可以查看我的AndroidSQLite查询游标吗?

时间:2019-02-07 23:31:10

标签: android sql sqlite android-studio android-sqlite

      Cursor cursor = db.rawQuery("SELECT * FROM fruits",null);

有效,但是一旦我开始添加以下限制:

      cursor = db.rawQuery("SELECT fruitName, color, 
       number FROM fruits WHERE color LIKE '%re%' AND fruitName LIKE 
      '%ppl%' AND number >=" + 5, null); 


       String[] fString = {"fruitName","color","number"};
        int[] tString = {R.id.fruit,R.id.color,R.id.number};

然后我在这里不断出现错误:

           ListAdapter adapter = new SimpleCursorAdapter(
            this,R.layout.newLayOutForSQL,cursor,
            fString,tString,0);

我在db.rawQuery中尝试了SELECT * FROM水果,它可以正常工作,但是限制不起作用

           SQLiteDatabase db;

db设置正确,所以不用担心

      cursor = db.rawQuery("SELECT fruitName, color, 
       number FROM fruits WHERE color LIKE '%re%' AND fruitName LIKE 
      '%ppl%' AND number >=" + 5, null); 


       String[] fString = {"fruitName","color","number"};
        int[] tString = {R.id.fruit,R.id.color,R.id.number};


           ListAdapter adapter = new SimpleCursorAdapter(
            this,R.layout.newLayOutForSQL,cursor,
            fString,tString,0);

预期:显示带有限制条件的结果 实际结果:错误

1 个答案:

答案 0 :(得分:0)

出现错误的原因是因为未指定*,因此排除了CursorAdapters必需的 _id 列。

按照:-

  

Cursor必须包含一个名为“ _id”的列,否则此类将不会   工作。此外,如果在以下情况下将MergeCursor与此类配合使用,将无法正常工作   合并的游标的“ _id”列中的值重叠。   [CursorAdapter](https://developer.android.com/reference/android/widget/CursorAdapter)( SimpleCursorAdapter是CursorAdapter的间接子类

尝试:-

cursor = db.rawQuery("SELECT _id, fruitName, color,
   number FROM fruits WHERE color LIKE '%re%' AND fruitName LIKE 
  '%ppl%' AND number >=" + 5, null); 

或者:-

cursor = db.rawQuery("SELECT * 
   FROM fruits WHERE color LIKE '%re%' AND fruitName LIKE 
  '%ppl%' AND number >=" + 5, null);