2

以下のコードが機能しないのはなぜですか? 検索条件に一致するものが多数あるにもかかわらず、ゼロ行が返されます。

フォームの単純なクエリはselect * from Table_1正常に機能し、正の数の行を返します

import cx_Oracle
def function_A (data):
    connection = cx_Oracle.connect('omitted details here')

    for index in range(len(data)):
        # connection is already open
        cursor = connection.cursor()

        query = "select * from Table_1 where column_1=:column1 and column_2=:column2 and column_3=:column3 and column_4=:column4"
        bindVars={'column1':data[index][3], 'column2':data[index][4], 'column4':data[index][5], 'column5':data[index][6]}
        cursor.execute(query, bindVars)
        cursor.arraysize = 256
        rowCount = 0
        resultSet = cursor.fetchall()
        if (resultSet != None):
            logger.debug("Obtained a resultSet with length = %s", len(resultSet))
            for index in range(len(resultSet)):
                logger.debug("Fetched one row from cursor, incrementing counter !!")
                rowCount = rowCount + 1
                logger.debug("Fetched one row from cursor, incremented counter !!")
            logger.debug("Successfully executed the select statement for table Table_1; that returned %s rows !!", rowCount)
            logger.debug("Successfully executed the select statement for table Table_1; that returned %s rows !!", cursor.rowcount)

マイナーなフォーマットの問題は無視してください。コードを実行しても、正の数の行が得られません。

コードは、python2.6 および互換バージョンの cx_Oracle を使用して IBM AIX で実行されています。

4

3 に答える 3

5

Oracle CX のカーソルオブジェクトには、読み取り専用のrowcountプロパティがあります。Rowcountfetch* メソッドで返される行数を返しています。

クエリが 5 行を生成するとすると、相互作用は次のようになります。

  1. 実行行数 = 0
  2. fetchone 行数 = 1
  3. fetchone 行数 = 2
  4. fetchall 行数 = 5

そうすることで、手動で追跡する必要がなくなります。クエリの問題は、最初にオフコースで解決する必要があります:)

于 2016-04-22T07:32:35.287 に答える
1

クエリに一致する行が 0 行あるため、クエリは 0 行を返します。WHERE 句から述語を削除するか、渡す値を変更してください。

変数に何もバインドしていないことに注意してcolumn3ください。また、カーソルによってフェッチされた行数が得られるため、bindVars繰り返している理由も完全にはわかりません。cursor.rowcount

通常、SELECT ステートメントが正しい結果を返さないと思われる場合は、コードを取得して、データベースに対して直接実行します。最初にすべての変数をバインドして、実際に実行しているものを正確に確認できるようにします。

于 2013-09-28T06:15:43.487 に答える