Only get 1st element of TABLE in SQLite

时间:2015-10-30 21:56:03

标签: android sqlite android-sqlite

I have this code:

public LinkedList<Product> getAllProduct(String idtask){
    LinkedList<Product> products = new LinkedList<Product>();
    Cursor localCursor = this.getWritableDatabase().rawQuery("SELECT * FROM "+TABLE_PRODUCT+" WHERE "+KEY_ID_PRODUCT_TASK+"='"+idtask+"'", null);
    Product product;
    if (localCursor.moveToFirst())
        do {
            product = new Product();
            product.setKEY_ID_UNIQUE_PRODUCT(localCursor.getString(0));
            product.setKEY_ID_PRODUCT_TASK(localCursor.getString(1));
            product.setKEY_ID_PRODUCT(localCursor.getString(2));
            product.setKEY_NAME_PRODUCT(localCursor.getString(3));
            product.setKEY_QTY_PRODUCT(localCursor.getString(4));
            product.setKEY_SIZE_PRODUCT(localCursor.getString(5));
            products.add(product);
        } while (localCursor.moveToNext());
    return products;
}

I only get 1 row, when with a particular IdTasks, I have to get 2 elements. Please Help me, I don't Know whats the problem. Because, when I debugging I add 2 products with this IdTask in the Table product, but when I try to get these products with this IdTask, I only get the first.

Here is the code with I add products int he table PRODUCT

public void addProduct(Product product)
{   
        SQLiteDatabase localSQLiteDatabase = getWritableDatabase();
        ContentValues localContentValues = new ContentValues();

        if (!isExistIDProduct(product.getKEY_ID_PRODUCT())){
            localContentValues.put(KEY_ID_PRODUCT_TASK, product.getKEY_ID_PRODUCT_TASK());
            localContentValues.put(KEY_ID_PRODUCT, product.getKEY_ID_PRODUCT());
            localContentValues.put(KEY_NAME_PRODUCT, product.getKEY_NAME_PRODUCT());
            localContentValues.put(KEY_QTY_PRODUCT, product.getKEY_QTY_PRODUCT());
            localContentValues.put(KEY_SIZE_PRODUCT, product.getKEY_SIZE_PRODUCT());
            localSQLiteDatabase.insert(TABLE_PRODUCT, null, localContentValues);        
        }
}

1 个答案:

答案 0 :(得分:1)

如果您使用的是重复使用相同产品ID的测试数据,则以下测试会阻止您添加第二行:

if (!isExistIDProduct(product.getKEY_ID_PRODUCT()))