sqlite cursor returning wrong value for a column

前端 未结 2 1694
挽巷
挽巷 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)

    0 讨论(0)
  • 2020-12-22 02:53

    Be careful with your datatypes in table definition because MySql datatypes are not supported in SQLite. Your varchar fields are supposed to be correct by type affinity but text is the correct way to store strings, not varchar or string.

    You defined also datetime which is not a good idea. Use text or integer (with timestamps) instead.

    0 讨论(0)
提交回复
热议问题