I am utilizing Pyspark in Databricks including inserting parametrized values into a SQL Server table through a SQL command.
Inserting parameters into SQL command (Pyspark):
sql = "INSERT INTO dbo.Validation VALUES ('{}','{}','{}','{}')".format(app,date,anomaly,value)
The SQL Server table has the following columns:
app = [varchar](255)
date [date]
anomaly= [varchar](255)
value = [nvarchar](max)
Dictionary as shown below:
value = {'pt_PT.UTF-8': [88], 'lt_LT.UTF-8': [24], 'fi_FI.UTF-8': [4], 'fr_BE.UTF-8': [4], 'nl_NL.UTF-8': [4]}
Printed SQL command:
INSERT INTO dbo.Validation VALUES ('TestApp','2020-05-06','LanguageAnomaly', '{'pt_PT.UTF-8': [88], 'lt_LT.UTF-8': [24], 'fi_FI.UTF-8': [4], 'fr_BE.UTF-8': [4], 'nl_NL.UTF-8': [4]}')
Error when executing SQL command:
com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near 'pt_PT'.
The issue I am encountering seems to be related to inserting the dictionary parameter "value". This maybe related to the apostrophes within the dictionary keys which the code cannot properly parse as a whole string.
How can I resolve this? This seems to work find if the dictionary key is a number value/int without quotes(').
Thanks.