I tried to find the string formats to write down a query for a database in Python from this post.
But when I tried to implement the following code
cursor = session.cursor()
query_1 = ("""Drop table PP_oap_KK_T.shipping_identifier1;\
Create table PP_oap_KK_T.shipping_identifier1 as (
sel
a.tracking_number,
shipping_company,
shipping_company_other,
Case when shipping_company_other is null then 0 else 1 end as Free_Text_YoNo,
Case when shipping_company like any ( '%other%','%None%') then 1 else 0 end as Ship_other_none_YN,
Case when Ship_other_none_YN = 0 and Free_Text_YoNo =0 then 1 else 0 end as Dropdown_YN,
Case when tracking_number like any ('%1%', '%2%' ,'%3%','%4%','%5%','%6%','%7%','%8%','%9%','%0%') then 1 else 0 end as Digit_YN,
Case when tracking_number like any ( '% %', '%#%', '%@%' , '%!%' , '%$%', '%^%', '%&%', '%*%', '%(%', '%)%', '%-%', '%+%', '%=%', '%,%', '%.%' , '%|%' , '%\%' , '%/%'
) then 1 else 0 end as SpecialCharacters_YN
from PP_oap_KK_T.inr_tv6 a
where Digit_YN = 1 and SpecialCharacters_YN =0 and character_length(tracking_number ) between 8 and 40
and atkc_open_date between '2017-06-01' and '2017-06-30'
and atkc_workflow_code in 'C'
and dispute_reason in 'INR'
sample 10000
) with data primary index(tracking_number); """)
cursor.execute(query_1)
But I am getting following error
Traceback (most recent call last):
File "C:\Users\pbuddharaju\Desktop\jsonTesting.py", line 34, in <module>
cursor.execute(query_1)
File "C:\Users\pbuddharaju\AppData\Local\Programs\Python\Python36\lib\site-packages\teradata\udaexec.py", line 745, in execute
self._execute(self.cursor.execute, query, params, **kwargs)
File "C:\Users\pbuddharaju\AppData\Local\Programs\Python\Python36\lib\site-packages\teradata\udaexec.py", line 789, in _execute
query = self.udaexec.config.resolve(query)
File "C:\Users\pbuddharaju\AppData\Local\Programs\Python\Python36\lib\site-packages\teradata\udaexec.py", line 577, in resolve
return self._resolve(value, sections, default, errorMsg)
File "C:\Users\pbuddharaju\AppData\Local\Programs\Python\Python36\lib\site-packages\teradata\udaexec.py", line 604, in _resolve
"escape '$' by adding another '$'.".format(value, error))
teradata.api.InterfaceError: ('CONFIG_ERROR', 'Unable to resolve "Drop table PP_oap_KK_T.shipping_identifier1;Create table PP_oap_KK_T.shipping_identifier1 as ( \nsel \na.tracking_number, \nshipping_company, \nshipping_company_other, \nCase when shipping_company_other is null then 0 else 1 end as Free_Text_YoNo, \nCase when shipping_company like any ( \'%other%\',\'%None%\') then 1 else 0 end as Ship_other_none_YN, \nCase when Ship_other_none_YN = 0 and Free_Text_YoNo =0 then 1 else 0 end as Dropdown_YN, \nCase when tracking_number like any (\'%1%\', \'%2%\' ,\'%3%\',\'%4%\',\'%5%\',\'%6%\',\'%7%\',\'%8%\',\'%9%\',\'%0%\') then 1 else 0 end as Digit_YN, \nCase when tracking_number like any ( \'% %\', \'%#%\', \'%@%\' , \'%!%\' , \'%$%\', \'%^%\', \'%&%\', \'%*%\', \'%(%\', \'%)%\', \'%-%\', \'%+%\', \'%=%\', \'%,%\', \'%.%\' , \'%|%\' , \'%\\%\' , \'%/%\' \n) then 1 else 0 end as SpecialCharacters_YN \nfrom PP_oap_KK_T.inr_tv6 a \nwhere Digit_YN = 1 and SpecialCharacters_YN =0 and character_length(tracking_number ) between 8 and 40 \nand atkc_open_date between \'2017-06-01\' and \'2017-06-30\' \nand atkc_workflow_code in \'C\' \nand dispute_reason in \'INR\' \nsample 10000 \n) with data primary index(tracking_number); ". Parameter not found: Invalid placeholder in string: line 10, col 70. If parameter substitution is not intended, escape \'$\' by adding another \'$\'.')
As we can see that we are getting '\n' as well as " \' " characters which should not happen in order to execute properly.
I also figured out that adding '\' to end of each line will eliminate the new line character but it is arduous work to do so for 20-30 queries.
Although previously I followed the format mentioned as an answer in the post, I still end up getting the "\'" error after escaping the single quotes in python code.
So I would like to know the best way to format the string so that errors are eliminated and also I can use the method frequently with ease.