Skip to content
Advertisement

Django sql statements with non english symbols

I have sql statement, with non english symbols in where

(sap_code variable contains russian text)

sap_code = 'ОВОЩИ – ФРУКТЫ'
sql_string = 'SELECT ' 
            'SAP_CODE, ' 
            'SKU, ' 
        'FROM INFO ' 
        f'WHERE ROWNUM < {settings.NQ_SQL_LIMIT}'
if sap_code:
    sql_string += f' AND SAP_CODE = {sap_code}'
with connections[db].cursor() as cursor:
    exec_result = cursor.execute(
        sql_string
    )

And I get such error django.db.utils.DatabaseError: ORA-00911: invalid character (oracle database)

Is there any way to fix that?

Advertisement

Answer

This is an Oracle limitation. You will need to quote your non-ASCII characters. I’ve changed your final fragment to double quotes and then quoted the internal string. That should resolve this issue if you’re dead-set on using raw SQL here.

sap_code = 'ОВОЩИ – ФРУКТЫ'
sql_string = 'SELECT ' 
             'SAP_CODE, ' 
             'SKU, ' 
             'FROM INFO ' 
             f'WHERE ROWNUM < {settings.NQ_SQL_LIMIT}'
if sap_code:
    sql_string += f" AND SAP_CODE = '{sap_code}'"
with connections[db].cursor() as cursor:
    exec_result = cursor.execute(
        sql_string
    )
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement