为什么循环运行不止一次?

时间:2011-12-04 01:45:59

标签: python oracle object loops for-loop

我为发布这么多代码而道歉,我无法在抽象的简化程序上重复这一行为,所以我不得不发布实际的代码。

目标是将cart表的内容放在ordersodetails表中。

实际发生的情况是,如果购物车中有一个条目,则订单表中将有十个以上的条目。我看到在insertSql之后执行的for循环的方式是它将在整个循环中运行一次并在中断处退出。也就是说,它会执行insertSql然后检索订单号ono,然后将类属性orderNumber设置为检索到的订单号ono,并将购物车设置为空。我错过了什么?

我正在使用Python 2.7以及模块cmdcx_Oracle

def oneClick(self,userLoggedIn):

    #this checks if there is a credit card available, if there isn't it denies one click check out
    sql = """
SELECT creditcardnumber, creditcardtype
FROM bs_members
WHERE userid = :userid
    """
    ds.execute(sql,userid=userLoggedIn)

    for row in ds:
        if row == (None,None):
            print """
One click check out is not available if you do not have a credit card on file.
"""

        else:
            break

    #this checks to see if the user has anything in the cart
    cartSql = """
SELECT DISTINCT userid FROM bs_cart WHERE userid = :userid
    """
    ds.execute(cartSql,userid=userLoggedIn)

    #this places the contents of the cart into the order tables
    insertSql = """
BEGIN
    INSERT INTO bs_orders (userid, ono, timePurchased)
        VALUES (:userid, orderNum_seq.NEXTVAL,(SELECT current_timestamp FROM dual));

    COMMIT;

    UPDATE bs_orders
        SET shipaddress = (SELECT address FROM bs_members WHERE userid = :userid)
            , shipcity = (SELECT city FROM bs_members WHERE userid = :userid)
            , shipstate = (SELECT state FROM bs_members WHERE userid = :userid)
            , shipzip = (SELECT zip FROM bs_members WHERE userid = :userid)
        WHERE userid = :userid
        AND timePurchased IN 
            (
            SELECT MAX(timePurchased)
                FROM bs_orders
                WHERE userid = :userid
                GROUP BY userid
            );

    INSERT INTO bs_odetails (ono, isbn, qty, price)
        SELECT orderNum_seq.CURRVAL, bs_cart.isbn, bs_cart.qty, bs_books.price
            FROM bs_orders, bs_cart, bs_books
            WHERE bs_orders.userid = :userid
            AND bs_cart.userid = :userid
            AND bs_books.isbn = bs_cart.isbn;

    DELETE FROM bs_cart
        WHERE userid = :userid;
END;
        """
    #if the cart has contents this for loop is executed

    for row in ds:
        ds.execute(insertSql,userid=userLoggedIn)
        sql = """
SELECT ono
    FROM bs_orders
    WHERE userid = :userid
    AND timePurchased IN
        (
        SELECT MAX(timePurchased)
            FROM bs_orders
            WHERE userid = :userid
            GROUP BY userid
        )
        """

        ds.execute(sql,userid=userLoggedIn)
        for row in ds:
            self.orderNumber = ''.join(str(i) for i in row)
            self.cartIsEmpty = False
        break

    #if the cart does not have any contents the user is told to try again
    else:
        print """
You have nothing in your cart!
            """
        self.cartIsEmpty = True

0 个答案:

没有答案