从Android SQLite获取价值并使用选择查询进行修改

时间:2015-04-24 18:16:39

标签: android sqlite

我正在尝试获取旧的QTY记录并向其添加新值,我希望您知道数据库中的qty是一个varchar,所以我创建了一个将其转换为整数的方法

这是 DataBAseHelper 方法

public void checkIfExcist(Context mCtx, String recordName,
        String ValueCheck, String changedQTY, String changedPrice,
        String changedPNAme) {

    DatabaseHelper mDbHelper = new DatabaseHelper(mCtx);
    SQLiteDatabase mDb = mDbHelper.getWritableDatabase();

    Cursor mCursor = mDb.rawQuery("SELECT * FROM " + SQLITE_TABLE
            + " WHERE " + recordName + " ='" + ValueCheck + "'", null);

    if (mCursor != null && mCursor.moveToFirst()) {

        Toast.makeText(mCtx, "Available", 3000).show();
        ContentValues values = new ContentValues();
        values.put(qty, changedQTY);
        values.put(price, changedPrice);
        mDb.update(SQLITE_TABLE, values, PRODUCT_NAME + "='" + changedPNAme
                + "'", null);

    } else {
        Toast.makeText(mCtx, "Not Available", 3000).show();
        /* record not exist */
    }

}

public int getQuantity(int Quantity, String pName, String tableName,
        String myQTY) {
    String theQTY = "nothing";
    String selectQuery = "SELECT " + pName + "FROM " + tableName
            + " WHERE " + myQTY + "='" + Quantity + "';";
    Cursor c = mDb.rawQuery(selectQuery, new String[] { "hello.txt" });
    if (c.moveToFirst()) {
        theQTY = c.getString(c.getColumnIndex(pName));
    }
    c.close();

    int convertedQTY = Integer.parseInt(theQTY);
    return Quantity + convertedQTY;
}

这就是我使用类 onClick方法

中的方法的方法
                int myQTY = Integer.parseInt(qty.getText().toString());
            String myTitle= m.getTitle().toString();
            String myCategory = m.getCategory().toString();
            int myCategoryN = m.getOrderNum();
            double myPrice = m.getPrice()*myQTY;

            int newMyQTY = myADapterDB.getQuantity(myQTY, "productName", "Country", "myqty");

            myADapterDB.checkIfExcist(activity, "productName", myTitle, newMyQTY+"", myPrice+"", myTitle);

0 个答案:

没有答案