0

I answered one of my previous questions, however, having fixed the issue, I am faced with an another issue regarding SQLServerException.

I am trying to read in data on an Azure SQLDB. I have successfully authenticated to the server however when I try to apply the function to read in data I get the following error:

com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near '5'

The following is more details on the error:

Py4JJavaError                             Traceback (most recent call last)
<command-3741352302548628> in readFromDb(processId, query)
      3   try:
----> 4     jdbcDF = (spark.read
      5       .format("jdbc")

/databricks/spark/python/pyspark/sql/readwriter.py in load(self, path, format, schema, **options)
    209         else:
--> 210             return self._df(self._jreader.load())
    211 

/databricks/spark/python/lib/py4j-0.10.9-src.zip/py4j/java_gateway.py in __call__(self, *args)
   1303         answer = self.gateway_client.send_command(command)
-> 1304         return_value = get_return_value(
   1305             answer, self.gateway_client, self.target_id, self.name

The code is as follows:

def readFromDb(processId, query):
  try:
    jdbcDF = (spark.read
      .format("jdbc")
      .option("driver", "com.microsoft.sqlserver.jdbc.SQLServerDriver")
      .option("url", f"jdbc:sqlserver://{DBServer}.database.windows.net;database={DBDatabase}")
      .option("user", DBUser)
      .option("query", query)
      .option("password", DBPword)
      .load()    
     )
    return jdbcDF
  except Exception as e:
    writeToLogs(processId,LogType.Error, EventType.FailReadFromDb, LogMessage.FailReadFromDb, errorType = ErrorType.FailReadFromDb)
    raise Error(f"{LogMessage.FailReadFromDb.value} ERROR: {e}")
  except:
    writeToLogs(processId,LogType.FailReadFromDb, EventType.FailReadFromDb, LogMessage.FailReadFromDb, errorType = ErrorType.FailReadFromDb)
    raise Error(f"{LogMessage.FailReadFromDb.value}")

Can someone let me know what the code generally means and best approach to fix it?

7
  • perhaps your query has a syntax error? Commented May 7, 2021 at 16:02
  • most probably that is an error in the query Commented May 7, 2021 at 17:10
  • @Patterson, did you check the query and resolved the error? Commented May 10, 2021 at 7:52
  • Yes I did. It would appear the problem was with the query as suggested by @mck and Alex Ott Commented May 10, 2021 at 8:39
  • @Patterson glad to hear that. Do you need I post it as answer and end this question? Commented May 11, 2021 at 0:53

1 Answer 1

1

Like @mac and Alex Ott said, the error is mostly caused by the query statements.

And we are glad to hear the error is resolved bye modify the query.

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

Comments

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.