0

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'

MCM
  • 1,479
  • 2
  • 17
  • 22
  • Show the full traceback of the error as properly formatted text in the question. – Michael Butscher Feb 02 '23 at 15:13
  • Thanks just also realized it. Added it in the post. Thanks for pointing out – MCM Feb 02 '23 at 15:15
  • 2
    Is ````calc.Database```` an instance of the class Database or is it referencing the Class itself? The self parameter is automatically populated if an instance is calling the method, it seems like you are not calling the method from an instance hence the seemingly missing argument. – jjislam Feb 02 '23 at 15:16
  • @jjislam: Thanks but how should I fix it? I call the instance Database to create the sqlite database and then defining the function using to_sql and try to fill it in the created database. Somehow I tried many things but failed – MCM Feb 02 '23 at 18:47

1 Answers1

1

Try this:

db.to_sql(df, "example_table")

seems like db is the instance and not the calc.Database

jjislam
  • 557
  • 3
  • 8
  • Great Thanks this is the point. Can you please give a description and maybe an alternatoive or mor pythonic way? – MCM Feb 03 '23 at 09:36