2

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?

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

4 Comments

thank you. However even when using sqlContext.sql I get the same error message. I'm looking into whether the "use" is causing an issue.
I have tried with db and got same error i.imgur.com/mSCj2ge.png. Don't use use db; while selecting using spark sql. It automatically detects the schema.
Instead of using db, you can do like this i.imgur.com/e1DItKv.png
I had to use without "use", also the dataframe returned is a pyspark one, so I had to use df = df.toPandas() to make the conversion. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.