WriteData

Passes a single row of values into a KnowledgeKube data source's table, either by adding a new row or updating an existing one.

Syntax

WriteData( NewData, RowID, DataSource, GenerateIndex, DateFormatString, OptimiseIndex );

Parameters

Parameter Name Type Description Optional? Default Value
NewData String A comma-separated list string containing the aliases of any columns you want to add data to. For example: "ProductNameAlias,ManufacturerAlias". No n/a
RowID String If the function is used to update an existing row, this argument must be a string containing the alias of your data table's primary key. If it's used to add a new row, leave this argument as an empty string. No n/a
DataSource String The name of the data source to update. No n/a
GenerateIndex Integer Determines whether the data source's index should be rebuilt after the new data is added. Yes 0/False
DateFormatString String This is only needed if the data being written contains one or more date columns. The value used for this parameter must be written exactly like this:
"{0:yyyy-MM-ddTHH':'mm':'ss}"
Yes (see Description) ""
OptimiseIndex Boolean Determines whether the data source index should be optimised. This has no effect if GenerateIndex is set to False. Yes (see Description) 0/False

Output

When used with a Microsoft SQL Server data source, the function will return a different string depending on the outcome:

  • If a new row was inserted, the string will contain the value assigned to the new row's numeric primary key. If the primary key is not numeric (e.g. it is a GUID), the function returns -1 instead.

If you want to retrieve a non-numeric primary key after adding a new row to your SQL Server data source, you can use a Stored Procedure to perform an INSERT operation, then have the procedure output the primary key.

  • If an existing row was updated, the string contains the value -1.
  • If you attempted to update a row using an invalid RowID argument, the string contains "False".

The function will return a value of -1 in all cases for non-SQL Server data sources.

Use Case

A rock band's management team wants to give fans of the band the ability to register contact details on the band's website. The contact details can then be entered into a prize draw to win signed copies of the band's new album. A series of questions are used to collect the contact information, and each question passes its response into a target model variable.

To contain the captured contact information, the management's KnowledgeKube administrator creates a database table consisting of four columns. The table is connected to the model using a data connection called FanDetailsDataConnect, and each column is given an alias whose name corresponds to one of the variables used to store contact details.

A list of the columns in the table, including their type and alias, is shown below:

Column Name Column Type Alias / Variable Name
PersonID Int (auto-incrementing, primary key) PersonIDAlias
Surname Varchar SurnameAlias
Forenames Varchar ForenameAlias
Email Varchar EmailAlias

When a fan has completed the contact form, they are asked to click a submit button. This button contains the following expression:

WriteData("ForenameAlias,SurnameAlias,EmailAlias", "", "FanDetailsDataConnect");

Because the second argument has been left as a blank string, the expression will always create a new entry in the data table. The person's surname, forenames and e-mail address will be added to the table and collectively assigned the next available value as the corresponding PersonID.

After the database and capture form are fully implemented, the first five entries created using the expression above might look like this:

PersonID Surname Forenames Email
1 Jane Aspinall jane.aspinall@yooboo.com
2 Margie Bowen margieb@gaga.com
3 Tina Chapman tidgytina@manoman.net
4 Bobby Davis b.davis@gaga.com
5 Christina Eckhart christina@ecky.com

In a real-world example, some kind of security measure would be needed to prevent duplication or 'spamming' of contact detail submissions. Additionally, there would have to be some kind of verification to ensure that e-mail addresses provided are valid. This falls outside the scope of our basic example, but you can expand your own expression and KnowledgeKube model to improve functionality as needed.