1. Are you ready for the Galaxy S20? Here is everything we know so far!

cursor.moveToFirst() returning false

Discussion in 'Android Development' started by Android Question, Mar 22, 2016.

  1. Android Question

    Thread Starter

    public class MyDBHandler extends SQLiteOpenHelper {

    int s=0;
    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "productDB3.db";
    private static final String TABLE_PRODUCTS = "products3";

    public static final String COLUMN_ID = "_id";
    public static final String COLUMN_TYPE = "type";
    public static final String COLUMN_QUANTITY = "quantity";
    public static final String COLUMN_WIDTH = "width";
    public static final String COLUMN_LENGTH = "length";
    public static final String COLUMN_THICK = "thick";

    public MyDBHandler(Context context, String name,
    SQLiteDatabase.CursorFactory factory, int version) {
    super(context, DATABASE_NAME, factory, DATABASE_VERSION);
    }

    @override
    public void onCreate(SQLiteDatabase db) {
    String CREATE_PRODUCTS_TABLE = "CREATE TABLE " +
    TABLE_PRODUCTS + "("
    + COLUMN_ID + " INTEGER PRIMARY KEY autoincrement," + COLUMN_TYPE
    + " TEXT," + COLUMN_WIDTH + " INTEGER," + COLUMN_LENGTH + " INTEGER," + COLUMN_THICK + " INTEGER,"
    + COLUMN_QUANTITY + " INTEGER" + ")";
    db.execSQL(CREATE_PRODUCTS_TABLE);

    }

    @override
    public void onUpgrade(SQLiteDatabase db, int oldVersion,
    int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRODUCTS);
    onCreate(db);

    }

    public void addProduct(String type,int width,int length,int thick,int quant) {


    ContentValues values = new ContentValues();
    values.put(COLUMN_TYPE,type);
    values.put(COLUMN_WIDTH,width);
    values.put(COLUMN_LENGTH,length);
    values.put(COLUMN_THICK,thick);
    values.put(COLUMN_QUANTITY,quant);

    SQLiteDatabase db = this.getWritableDatabase();

    db.insert(TABLE_PRODUCTS, null, values);
    db.close();
    }

    public int findProduct(String type,int width,int length,int thick) {

    String query = "SELECT quantity FROM products3 WHERE type='" + type +"' AND width=" + width + " AND length=" +
    length+" AND thick=" + thick + ";";
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(query, null);

    if (cursor.moveToFirst()) {

    cursor.moveToFirst();
    s=cursor.getInt(0);

    cursor.close();

    db.close();
    return s;
    }
    return 0;
    }

    public boolean deleteProduct(String type,int width,int length,int thick) {

    boolean result = false;

    SQLiteDatabase db = this.getWritableDatabase();
    db.execSQL("DELETE FROM products3 WHERE type='" + type +"' and width=" + width + " and length=" +
    length+" and thick=" + thick );

    result=true;
    db.close();
    return result;
    }



    }
     


  2. Deleted User

    Deleted User Guest
    Thread Starter

Loading...

Share This Page

Loading...