Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

android - sqlite cursor returning wrong value for a column

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

I am adding data in sqlite like this

  void addObjects(Objects object) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();

        values.put(OBJECT_ID,"10" ); // OBJECT Name
        values.put(OBJECT_NAME, "H"); // OBJECT Name
        values.put(OBJECT_URL, "http://api.androidhive.info/images/sample.jpg"); // OBJECT URL
        values.put(OBJECT_TYPE, "image"); // Contact type
        System.out.println(values);

        // Inserting Row
        db.insert(TABLE_OBJECTS, null, values);
        db.close(); // Closing database connection
    }

the result of log values www.i I/System.out﹕ TYPE=image NAME=H ID=10 URL=http://api.androidhive.info/images/sample.jpg

however when I get all the result I have url = null

public List<Objects> getAllObjects() {
    List<Objects> Objectslist = new ArrayList<Objects>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_OBJECTS;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            Objects object = new Objects();
            Log.d("OBJECT_URL", OBJECT_URL);

            object.setId(cursor.getInt(cursor.getColumnIndex(OBJECT_ID)));
            object.setName(cursor.getString(cursor.getColumnIndex(OBJECT_NAME)));
            object.setUrl(cursor.getString(cursor.getColumnIndex(OBJECT_URL)));
            // Adding contact to list
            Objectslist.add(object);
        } while (cursor.moveToNext());
    }

This is main activity I am getting the result of sqlite

List<Objects> Objects = db.getAllObjects();
    Log.d("he","tttttt");
    System.out.println(Objects);
    DBadapter adapter = new DBadapter(getApplicationContext(), R.layout.grid_item_layout, Objects);

this is the result of system outprint

[Objects{id=10, name='http://api.androidhive.info/images/sample.jpg', url='null', type='null', category='null'}, Objects{id=10, name='http://api.androidhive.info/images/sample.jpg', url='null', type='null', category='null'}

this is full sqlite class

     public class DatabaseHandler extends SQLiteOpenHelper {

// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;



    // Contacts table name
    private static final String TABLE_OBJECTS = "OBJECTS";
    private static final String OBJECT_ID="ID";
    private static final String OBJECT_NAME="NAME";
    private static final String OBJECT_TYPE ="TYPE";
    private static final String OBJECT_URL ="URL";
    private static final String OBJECT_CATEGORY ="CATEGORY";
    private static final String OBJECT_DATE_ADDED ="DATE_ADDED";
    private static final String OBJECT_USERS_ID ="USERS_ID";
    private static final String OBJECT_HIDE_YN ="HIDE_YN";


    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    // Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db){
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_OBJECTS);

        String CREATE_OBJECTS_TABLE ="CREATE TABLE " +TABLE_OBJECTS+"("
       + OBJECT_ID +" INTEGER PRIMARY KEY AUTOINCREMENT,"
       + OBJECT_NAME +" VARCHAR(100) NULL,"
        + OBJECT_TYPE +" VARCHAR(10) NULL,"
        + OBJECT_URL +" VARCHAR(1000) NULL,"
        +OBJECT_CATEGORY +" VARCHAR(50) NULL,"
        +OBJECT_DATE_ADDED +" DATETIME NULL,"
        +OBJECT_USERS_ID +" NUMERIC(10) NULL,"
        +OBJECT_HIDE_YN +" VARCHAR(1) NULL) ";
        db.execSQL(CREATE_OBJECTS_TABLE);
    }
    // Upgrading database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_OBJECTS);

        // Create tables again
        onCreate(db);
    }

    /**
     * All CRUD(Create, Read, Update, Delete) Operations
     */

    // Adding new contact
    void addObjects(Objects object) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();

        values.put(OBJECT_ID,"10" ); // OBJECT Name
        values.put(OBJECT_NAME, "H"); // OBJECT Name
        values.put(OBJECT_URL, "http://api.androidhive.info/images/sample.jpg"); // OBJECT URL
        values.put(OBJECT_TYPE, "image"); // Contact type
        System.out.println(values);

        // Inserting Row
        long id = db.insert(TABLE_OBJECTS, null, values);
        System.out.println( "id:"+id);
        db.insert(TABLE_OBJECTS, null, values);
        db.close(); // Closing database connection
    }
  //  Objects object=new Objects(String );

    // Getting single contact
    Objects geturl(String name) {
        SQLiteDatabase db = this.getReadableDatabase();
        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);
        if (cursor != null)
            cursor.moveToFirst();

        Objects object = new Objects(cursor.getString(0),cursor.getString(1),
                cursor.getString(2),cursor.getString(3));

        return object;
    }

    // Getting All Contacts
    public List<Objects> getAllObjects() {
        List<Objects> Objectslist = new ArrayList<Objects>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_OBJECTS;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Objects object = new Objects();
                Log.d("OBJECT_URL", OBJECT_URL);

                object.setId(cursor.getInt(cursor.getColumnIndex(OBJECT_ID)));
                object.setName(cursor.getString(cursor.getColumnIndex(OBJECT_NAME)));
                object.setUrl(cursor.getString(cursor.getColumnIndex(OBJECT_URL)));
                // Adding contact to list
                Objectslist.add(object);
            } while (cursor.moveToNext());
        }

        // return contact list
        return Objectslist;
    }

    // Updating single contact
    public int updateContact(Objects object) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(OBJECT_NAME, object.getName()); // OBJECT Name
        values.put(OBJECT_URL, object.getUrl()); // OBJECT URL
        values.put(OBJECT_TYPE, object.getType()); // Contact type
        values.put(OBJECT_CATEGORY, object.getCategory()); // Contact category

        // updating row
        return db.update(TABLE_OBJECTS, values, OBJECT_ID + " = ?",
                new String[] { String.valueOf(object.getId()) });
    }

    // Deleting single contact
    public void deleteContact(Objects objects) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_OBJECTS, OBJECT_ID + " = ?",
                new String[] { String.valueOf(objects.getId()) });
        db.close();
    }


    // Getting contacts Count
    public int getObjectsCount() {
        String countQuery = "SELECT  * FROM " + TABLE_OBJECTS;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        cursor.close();

        // return count
        return cursor.getCount();
    }

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...