【问题标题】:Getting errors when trying to insert data into a table in Oracle尝试将数据插入 Oracle 中的表时出错
【发布时间】:2011-11-21 19:51:48
【问题描述】:

我正在使用 python 2.7 和 cx_Oracle 模块。当我尝试运行以下查询时:

UPDATE bs_cart
    SET qty = qty + :moreBooks
    WHERE userid = :userID 
    AND isbn = :bookNumber;

IF SQL%ROWCOUNT = 0 THEN
    INSERT INTO bs_cart
    (userid,isbn)
    VALUES
    (:userID,:bookNumber)

使用来自cx_OracleCursor.execute() 我收到以下错误:

DatabaseError: ORA-00911: invalid character

当我把它放在 SQL plus 中时,它说:

SP2-0734: unknown command beginning "IF SQL%ROW..." - rest of line ignored.

如果用户已经在购物车中拥有所选书籍,我将尝试创建购物车 UPDATE,如果购物车中没有他们想要的书籍的当前副本,我将尝试创建 INSERT

执行方法如下:

ds.execute(sql,moreBooks=howMany,userID=userLoggedIn,bookNumber=booksToBuy)

每个参数都是用户使用rawinput() 生成的,然后根据正则表达式进行检查。

【问题讨论】:

    标签: oracle cx-oracle ora-00911


    【解决方案1】:

    您需要将您的语句括在begin/end Oracle 块中。

    类似:

    BEGIN
      UPDATE bs_cart
      SET qty = qty + :moreBooks
      WHERE userid = :userID 
      AND isbn = :bookNumber;
    
      IF SQL%ROWCOUNT = 0 THEN
         INSERT INTO bs_cart (userid,isbn)
         VALUES (:userID,:bookNumber);
    END;
    

    【讨论】:

    • 我附上了BEGINEND,但现在我收到了一个错误,上面写着PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following: ( begin case declare else elseif end exit . . .。它提到 END 是它所期望的,那么为什么它会失败?
    • 您需要在 INSERT 语句的末尾加上一个分号,并在 END 之前加上一个“END IF”。
    • 当我添加 END IF;连同你的建议,它奏效了。感谢 Pablo 和 Griffey。
    猜你喜欢
    • 2015-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多