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'

4
  • Show the full traceback of the error as properly formatted text in the question. Commented Feb 2, 2023 at 15:13
  • Thanks just also realized it. Added it in the post. Thanks for pointing out Commented Feb 2, 2023 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. Commented Feb 2, 2023 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 Commented Feb 2, 2023 at 18:47

1 Answer 1

1

Try this:

db.to_sql(df, "example_table")

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

Sign up to request clarification or add additional context in comments.

1 Comment

Great Thanks this is the point. Can you please give a description and maybe an alternatoive or mor pythonic way?

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.