In this we are dynamically creating and executing SQL insert statements to add rows from a DataFrame to a Snowflake table. The DataFrame df may contain None values, representing SQL NULL. We iterate over rows of the DataFrame, and for each value in a row, we determine whether it is None. If it’s None, we append ‘NULL’ (without quotes) to our values list; if it’s a numeric value, we convert it to a string and append it as-is; and if it’s another non-numeric value, we convert it to a string and append it enclosed in single quotes. After processing all values in a row, we join them using commas and insert them into the predefined SQL string. If an error occurs while executing the SQL statement, it gets caught and printed. The code can indeed be modified to handle None values and ensure that if a column value is None, it is represented as NULL without quotes in the SQL string.
Suppose you have a DataFrame df like this:
For the first row, the produced SQL string will be something like:
For the second row, it will be:
For the third row, it will be:
In each case, NULL values are not quoted, and string values are quoted, maintaining the integrity of other data types.
Refer more on python here : Python