0

I understand that the documented way to insert data into a table looks like

```class Table(db.Model):
       __tablename___ = 'table'
       id = db.Column(db.Integer, primary_key=True)
       data = db.Column(db.String(50)
    ...

    insert = Table(id = '0', data = 'new data')```

However, I am working on a project that has multiple tables all with different columns, lengths, and data. I have worked out how to get the dynamic data into a dict, prepped to create rows. Below is my actual code:

def load_csv_data(self, ctx):
        data_classes = [Locations, Scents, Classes]
        data_tables = ['locations', 'scents', 'classes']

        tables = len(data_tables)
        for i in range(tables):
            with open('./development/csv/{}.csv'.format(data_tables[i]), newline='') as times_file:
                times_reader = csv.reader(times_file, delimiter=',', quotechar='|')
                for row in times_reader:
                    data_columns = data_classes[i].__table__.columns
                    columns = len(data_columns)
                    insert_data = {}
                    for col in range(columns):
                        row_key = data_columns[col].key
                        row_value = row[col]
                        insert_data.update({row_key: row_value})

The challenge I am having is finding a way to do the actual insert based on these dynamic params. So if the above returns:

insert_data = {val1: val2, val3: val4, val5: val6}

I would like to convert this to:

insert = Table(val1='val2', val3='val4', val5='val6)

Everything I have tried so far has issued a __init__() missing 2 required positional arguments: error.

Anyone have any thoughts on how I might accomplish this?

2

0

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.