Home > database >  Retrieve df from spark.sql : [PARSE_SYNTAX_ERROR] Syntax error at or near 'SELECT'
Retrieve df from spark.sql : [PARSE_SYNTAX_ERROR] Syntax error at or near 'SELECT'

Time:12-15

I'm using a databricks notebook and I'd like to retrieve a dataframe from an SQL execution in Spark. I have:

statement = f""" USER {db}; SELECT * FROM {table}
"""

df = spark.sql(statement)
display(df) 

However, unlike when I fire off the same statement in an SQL cell in the notebook, I get the following error:

[PARSE_SYNTAX_ERROR] Syntax error at or near 'SELECT': extra input 'SELECT'(line 1...

Where am I going wrong?

CodePudding user response:

I tried to reproduce the same in my environment and got below results:

This my sample demo table Persons.

enter image description here

Create dataframe by using this code as shown in the below image.

df = sqlContext.sql("select * from Persons")
display(df)

enter image description here

  • Related