Keep getting DatabaseError: ORA-00933: SQL command not properly ended with no explanation

53 Views Asked by At

I am trying to read from an Oracle DB in python using this code:

conn: SqlConnection
# conn_info = DotenvSqlConnection.get_connection_info(add_env_to_connection_id(ConnectionList.Oracle, forced_env_type=EnvironmentType.Dev))
conn_info = conn.get_connection_info(add_env_to_connection_id(ConnectionList.Oracle, forced_env_type=EnvironmentType.Dev))
host_Or= conn_info.host
service_or=conn_info.service
user_id_or=conn_info.user
password_or= conn_info.password
port_or = conn_info.port


print(host_Or, service_or, user_id_or, password_or, port_or)
connection_string = f"{user_id_or}/{password_or}@{host_Or}:{port_or}/{service_or}"
cnxn = cx_Oracle.connect(connection_string)
cursor = cnxn.cursor()

cursor = cnxn.cursor()
table = 'myTable'
account_query = f'''
                SELECT a.*
                FROM my_user.{table} a
                FETCH FIRST 4333 ROWS ONLY
                ORDER BY a.ACCNT_ID;
                '''
cursor.execute(account_query) 
row = cursor.fetchone() 

# Print the count value (number of rows)
if row:
    row_count = row[0]
    print("Number of rows:", row_count)
else:
    print("No rows found.")

But I keep getting the following error:

----> 9 cursor.execute(account_query) 
     10 row = cursor.fetchone() 
     12 # Print the count value (number of rows)

DatabaseError: ORA-00933: SQL command not properly ended

I have tried many ways to get around it but it keeps showing up. any advice would be great thanks!

1

There are 1 best solutions below

0
Littlefoot On

Try to rewrite query. Take two steps:

First (generally speaking), remove terminating semi-colon:

account_query = f'''
                SELECT a.*
                FROM my_user.{table} a
                FETCH FIRST 4333 ROWS ONLY
                ORDER BY a.ACCNT_ID;         --> this one

                '''

Second, rewrite query so that ORDER BY comes first and FETCH next:

account_query = f'''
                SELECT a.*
                FROM my_user.{table} a
                ORDER BY a.ACCNT_ID
                FETCH FIRST 4333 ROWS ONLY
                '''