sqlite cursor returning wrong value for a column

前端 未结 2 1700
挽巷
挽巷 2020-12-22 02:14

why I am getting from the database null from column url ?

I am adding data in sqlite like this

  void addObjects(Objects o         


        
2条回答
  •  悲&欢浪女
    2020-12-22 02:39

    In your Objects geturl(String name) method , change

    Cursor cursor = db.query(TABLE_OBJECTS, new String[] {OBJECT_URL,
                    OBJECT_NAME , OBJECT_CATEGORY, OBJECT_TYPE }, OBJECT_NAME + "=?",
                    new String[] { String.valueOf(name) }, null, null, null, null);
    

    to

    Cursor cursor = db.query(TABLE_OBJECTS, new String[] {OBJECT_ID,
                    OBJECT_NAME , OBJECT_URL, OBJECT_TYPE }, OBJECT_NAME + "=?",
                    new String[] { String.valueOf(name) }, null, null, null, null);
    

    Supposing that your Objects class has the following structure:

    public class Objects {
        private String id;
        private String name;
        private String url;
        private String type;
    
        // methods
    }
    

    the tableColumns argument (new String[] {OBJECT_ID,OBJECT_NAME , OBJECT_URL, OBJECT_TYPE }) that you pass to the db.query() method has to respect the same order as in your Objects class (first the id, 2nd name, 3rd url, 4th type)

提交回复
热议问题