0

I am trying to run a script once per day at a specific time. The script is looking to get stock market data from an API and then append the data into the sqlite3 database I have already created. Within my sqlite3 database I have one table per asset whose data I am retrieving and updating daily for the most recent day's data.

I am running into the issue that my script works fine manually from my IDE, but when I turn it into a .exe file using pyinstaller --onefile dailydataupdate.py and test the .exe file in my terminal I get the following error:

  File "DailyDataUpdate.py", line 51, in <module>
  File "DailyDataUpdate.py", line 47, in todaydata
  File "pandas/core/generic.py", line 2987, in to_sql
  File "pandas/io/sql.py", line 695, in to_sql
  File "pandas/io/sql.py", line 2187, in to_sql
  File "pandas/io/sql.py", line 838, in create
  File "pandas/io/sql.py", line 1871, in _execute_create
sqlite3.OperationalError: table "EURUSD" already exists
[23130] Failed to execute script 'DailyDataUpdate' due to unhandled exception!

This is the script I am running:

import sqlite3 as db
import pandas as pd
import tradermade as tm
import datetime
from tapy import Indicators

assets = [ LIST OF ASSETS ]

conn = db.connect("MarketData.db")
c = conn.cursor()

def todaydata():
    tm.set_rest_api_key("[MY API KEY]")

    for x in assets:
        table_name = x

        request = tm.historical(
            currency=str(x),
            date=datetime.date.today(),
        )

        df = pd.DataFrame(request)
        df = df.rename(columns={"date": "dateold"})
        df['date'] = df['dateold'].dt.date
        first_column = df.pop('date')
        df.insert(0, 'date', first_column)
        df = df.drop(['instrument', 'dateold'], axis=1)

        i = Indicators(df)
        i.fractals()
        df = i.df

        df.to_sql(name=table_name,con=conn,if_exists='append',index=False)
    
    return

todaydata()
conn.commit()

There could be something wrong with my code, but I can't understand why when I manually run this script from my IDE it works fine and my tables get updated but running from the .exe file it fails.

jbaker3004
  • 11
  • 2
  • I recently discovered that table name argument is [case _sensitive_](https://github.com/pandas-dev/pandas/issues/22821) in the `to_sql` method for sqlite database. It doesn't explain why it works one way and not the other, but it might be worth investigating. – DinoCoderSaurus Jan 15 '23 at 15:34

0 Answers0