0

I have a problem with creating SQL query for Oracle database using Python. I want to bind string variable and it does not work, could you tell me what am I doing wrong? This is my code:

import cx_Oracle

dokList = []

def LoadDatabase():
    conn = None
    cursor = None
    try:
        conn = cx_Oracle.connect("login", "password", "localhost")

        cursor = conn.cursor()

        query = "SELECT * FROM DOCUMENT WHERE DOC = :param"

        for doknumber in dokList:

            cursor.execute(query, {'doknr':doknumber})
            print(cursor.rowcount)

    except cx_Oracle.DatabaseError as err:
        print(err)
    finally:
        if cursor:
            cursor.close()
        if conn:
            conn.close()

def CheckData():

    with open('changedNamed.txt') as f:
        lines = f.readlines()

        for line in lines:
            dokList.append(line)

CheckData()
LoadDatabase()

The output of cursor.rowcount is 0 but it should be number greater than 0.

1 Answer 1

1

You're using a dictionary ({'doknr' : doknumber}) for your parameter, so it's a named parameter - the :param needs to match the key name. Try this:

query = "SELECT * FROM DOCUMENT WHERE DOC = :doknr"
for doknumber in dokList:
        cursor.execute(query, {'doknr':doknumber})
        print(cursor.rowcount)

For future troubleshooting, to check whether your parameter is getting passed properly, you can also try changing your query to "select :param from dual".

Sign up to request clarification or add additional context in comments.

2 Comments

Okay, I changed it but again the value of cursor.rowcount is still 0, and select :doknr from dual also didn't work...
rowcount would be for DML. For queries, use a SELECT COUNT to get the number of rows.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.