I have the following code in two scripts called Playing_DB_Around.py with the following code:
import data_base.Calc_Func as calc
import pandas as pd
df = pd.DataFrame({'Name': ['John', 'Jane', 'Jim'], 'Age': [25, 30, 35]})
db = calc.Database("example_db")
calc.Database.to_sql(df, "example_table")
This code loads a own written bib which is in the script Calc_Func.py where the code is:
from sqlalchemy import create_engine
class Database:
def __init__(self, db_file):
self.engine = create_engine(f'sqlite:///{db_file}')
def to_sql(self, df, table_name):
df.to_sql(table_name, self.engine, if_exists='replace', index=False)
When executing Playing_DB_Around.py I receive the following error message. I am confused the class and the execution in one script it seems to work. I tried many things but somehow cant get it to run.
Error message.
Traceback (most recent call last): File "Playing_DB_Around.py", line 9, in calc.Database.to_sql(df, "example_table") TypeError: to_sql() missing 1 required positional argument: 'table_name'
CodePudding user response:
Try this:
db.to_sql(df, "example_table")
seems like db is the instance and not the calc.Database