Inserting table Data into database

In perspective we tried below code to fetch the value from table dynamically getting updated and insert these values into the table in database. But it fetches only the last row of the table and enter that value in the table in database.can anyone help us in fixing this issue.thanks in advance

tabledata = self.getSibling(“Table”).props.data
for row in system.dataset.toPyDataSet(tabledata):
column1 = row[‘Test_Parameter’]
column2 = row[‘Actual’]
system.db.runPrepUpdate(“INSERT INTO Process_Table (Parameter_ID, Process_value) VALUES (?,?)”, [column1, column2])

Your indentation is important here. Use three tick marks like this ``` before and after your code to preserve formatting.

it seems like not a indenting issue,the selected row is getting inserted into the DB instead of inserting all the rows in the table.

Does your code look like this?

tabledata = self.getSibling("Table").props.data
for row in system.dataset.toPyDataSet(tabledata):
    column1 = row['Test_Parameter']
    column2 = row['Actual']
    system.db.runPrepUpdate("INSERT INTO Process_Table (Parameter_ID, Process_value) VALUES (?,?)", [column1, column2])

Or more like this?

tabledata = self.getSibling("Table").props.data
for row in system.dataset.toPyDataSet(tabledata):
    column1 = row['Test_Parameter']
    column2 = row['Actual']
system.db.runPrepUpdate("INSERT INTO Process_Table (Parameter_ID, Process_value) VALUES (?,?)", [column1, column2])

The behaviour you want is the behaviour of the first block of code: loop over all rows, get the values and update the table for each row.

The behaviour you describe is what would happen with wrong indentation like in the second code block: loop over all rows, get the values one by one, store the values each time in a variable, and update the database once with the last set of values (i.e. with the last row).

This is why @zacht said indentation is important, and why you should have put that code in a code block.

4 Likes

Sorry for the late reply. It is working now…Thanks very much for your support.