I'm trying to insert some data on a table from pyspark calling a variable but I get the error below when I run it. My table is defined with one column and the data type for that column is string. I hope someone have an answer for this, thanks
'''
HORA = "Hello"
spark.sql('''
INSERT INTO ws_fcd_analitica.cu_portafolio_fenix_bitacora_snap1
VALUES (''' HORA ''')
''')
'''
error
'''
AnalysisException: cannot resolve Hello given input columns: []; line 3 pos 12;enter code here
'InsertIntoStatement 'UnresolvedRelation [ws_fcd_analitica, cu_portafolio_fenix_bitacora_snap1], [], false, false, false
- UnresolvedInlineTable [col1], [['Hello]]
'''
CodePudding user response:
This happening because the value you have provided is not enclosed in quotes to be treated as a string and not as a column name change it as below
HORA = "Hello"
spark.sql('''
INSERT INTO ws_fcd_analitica.cu_portafolio_fenix_bitacora_snap1
VALUES ("''' HORA '''")
''')
same thing with f string
HORA = "Hello"
spark.sql(f"""
INSERT INTO ws_fcd_analitica.cu_portafolio_fenix_bitacora_snap1
VALUES ('{HORA}')
""")