Skip to content
  • There are no suggestions because the search field is empty.

DataHub ODBC Scripting using a non-auto-incrementing primary key

I am trying to use the DataHub ODBC Scripting Tutorial1 (ODBCTutorial1.g) script and have modified it to work with my table's structure. My database has a non-auto-incrementing primary key and the .thread.Insert() command inside the writeData() method does not work.  For example, I have added a DateTime column named DateAndTime to my table as the primary key and I assign it to the row class like this:
row.DateAndTime = timestamp;

When the .thread.Insert() method is called, the insert does not succeed and gets caught in the "catch" block of code. What am I doing wrong?

This is happening because the default way for the script to handle the Insert assumes that you are using an auto-incrementing primary key.  In cases such as these, the primary key field does not need to be included in the SQL INSERT statement since the database will take care of it. This is the way the DataHub scripting handles inserts by default, but will cause errors in cases where the primary key field is not auto-generated by the database. To get around this, you can add the following two lines to the script inside the writeData() method before the call to .thread.Insert():

// Remove the primary key definition so the primary key will be included in an insert call

.tableclass.__primary_key = nil;

.thread.GetInsertFormat(.tableclass);

This will make the script think that there is no primary key (even though there is one on the database side) - and will avoid the case where it omits your DateAndTime field in the INSERT statement.