question

Scarlett X avatar image
0 Likes"
Scarlett X asked Scarlett X commented

Update last data

I have a SQL data table with 20 data fields, and I have used DatabaseConnector to import the data to the global table.

Please tell me how to use the Processflow function to update only the latest data(row) from SQL every second and add it to the global Table?

FlexSim 22.1.0
database
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

1 Answer

·
Felix Möhlmann avatar image
0 Likes"
Felix Möhlmann answered Scarlett X commented

The following code gets the last row from the specified database table and adds it to the defined target table. You will have to adjust the connection name, the table names and, depending on the database you are reading from, the row identifier that is used select the last row ("ID" in this case).

Database.Connection con = Database.Connection("DBConnector1");
con.connect();
if(con.isConnected)
{
    // Get last row from database table
    Database.ResultSet result = con.query("SELECT * FROM Table1 WHERE ID = (SELECT MAX(ID) FROM Table1)");

    // Check if number of fields matches table columns and the result is not empty
    int entries = result.numFields;
    Table target = Table("GlobalTable1");
    if(result.fetchNext() && entries == target.numCols)
    {
        // Add new row and write values to it
        target.addRow();
        int row = target.numRows;
        for(int i = 1; i <= entries; i++)
        {
            target[row][i] = result[i];
        }
    }
}

You can loop a token through a Custom Code activity with this code every second.

1669199104613.png


1669199104613.png (5.8 KiB)
· 1
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Scarlett X avatar image Scarlett X commented ·
Thank you!!!!!
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.