0

I'm getting the following error on the second iteration of t in tickers:

Traceback (most recent call last):
  File "D:\Python\GetSharePrices\main.py", line 10, in <module>
    for t in tickers:
pyodbc.ProgrammingError: No results.  Previous SQL was not a query.

If I remove the last two statements the entire script runs as expected. However, when I try to execute the cursor only the first row is committed before I get the error.

import yfinance as yf
import pyodbc as py


conn = py.connect('Driver={SQL Server}; Server=ORCHARD; Database=APPLE; Trusted_Connection=yes;')
cursor = conn.cursor()
sql = 'SELECT ticker FROM tickers'
tickers = cursor.execute(sql)
insert_sql = 'INSERT INTO share_price VALUES (?, ?, ?)'
for t in tickers:
    ticker = t[0]
    tickerData = yf.Ticker(ticker)
    tickerDf = tickerData.history(period='1d', start='2021-10-21', end='2021-10-21')
    for index, row in tickerDf.iterrows():
        price = row['Open']
        print(ticker, index, price)
        cursor.execute(insert_sql, (ticker, index, price))
    cursor.commit()

1 Answer 1

1

You didn't fetch the results. Do - tickers = cursor.execute(sql).fetch_all()

Assuming the table size is reasonable otherwise fetch a chunk at a time.

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.