sqlite游标返回错误的列值

时间:2015-11-18 20:43:48

标签: android sqlite

为什么我从列null的数据库url获取?

我正在像这样添加sqlite中的数据

  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
    }

日志值www.i I/System.out﹕ TYPE=image NAME=H ID=10 URL=http://api.androidhive.info/images/sample.jpg

的结果

然而,当我得到所有结果时,我有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());
    }

这是我获得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);

这是系统外印的结果

[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'}

这是完整的sqlite类

     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();
    }

}

2 个答案:

答案 0 :(得分:1)

Objects geturl(String name)方法中,更改

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);

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);

假设您的对象类具有以下结构:

public class Objects {
    private String id;
    private String name;
    private String url;
    private String type;

    // methods
}

传递给tableColumns方法的new String[] {OBJECT_ID,OBJECT_NAME , OBJECT_URL, OBJECT_TYPE }参数(db.query())必须遵守与Objects类相同的顺序(首先是id,第二个{ {1}},第3 name,第4 url

答案 1 :(得分:1)

请谨慎使用表定义中的数据类型,因为SQLite不支持MySql数据类型。 type affinity应该认为您的varchar字段是正确的,但是text是存储字符串的正确方法,而不是varcharstring

您还定义了datetime,但这不是一个好主意。改用textinteger(带时间戳)。