0

i am have a variable (exchanges) and I loop thru the values in my code to change the path for the output files for each exchange. When I also try to use this string substitution in the SQL block, it doesnt understand it. can someone suggest what is wrong? Here is the error I am getting

pandas.io.sql.DatabaseError: Execution failed on sql '
    f'SELECT [Ticker], [Date], [Open], [High], [Low], [Close], [Volume] FROM olaptraderv3.dbo.{ex} order by Ticker'
    ': ('42000', "[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Could not find stored procedure 'f'. (2812) (SQLExecDirectW)")

Here is the relevant part of the code:

import pandas as pd

pd.options.mode.chained_assignment = None  # default='warn'
import pyodbc
import talib
import os
from talib import  (AD,ADOSC,WILLR)

exchanges = ["BATS","US","V"]

for ex in exchanges:
path = f'H:\\EOD_DATA_RECENT\\INDICATORS\\FROM-SQL-SOURCE\\{ex}\\'

DB_READ = {'servername': 'XYZ\XYZ',
  'database': 'olaptraderv3'}

conn = pyodbc.connect('DRIVER={SQL Server};SERVER=' + DB_READ['servername'] + ';DATABASE=' +  DB_READ['database'] + ';Trusted_Connection=yes')

sql = """
f'SELECT [Ticker], [Date], [Open], [High], [Low], [Close], [Volume] FROM olaptraderv3.dbo.{ex} order by Ticker'
"""
df = pd.read_sql(sql, conn)

1 Answer 1

1

You need to have the format 'f' outside of the string.

sql = f"""
'SELECT [Ticker], [Date], [Open], [High], [Low], [Close], [Volume] FROM olaptraderv3.dbo.{ex} order by Ticker'
"""

I highly discourage doing string formatting/substitution with SQL as it is not sanitized and can be abused.

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.