-1

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?

Headmaster
  • 2,008
  • 4
  • 24
  • 51
  • 1
    Try national character literals, `N'ОВОЩИ'`, and nchar data type. – jarlh Aug 18 '20 at 09:13
  • Does this answer your question? https://stackoverflow.com/questions/38363566/trouble-with-utf-8-characters-what-i-see-is-not-what-i-stored –  Aug 18 '20 at 09:21

1 Answers1

1

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
    )
kerasbaz
  • 1,774
  • 1
  • 6
  • 15