Hi all,
Beginner question.
What is the syntax/appropriate method to update an entry in a self-defined CPS table? I am able to insert values, but the method/syntax for updating specific columns is not clear.
Thanks to some helpful posts I have the following already that simply checks my test table ( a table defined under the scripting section as a table with a single string column called "Description") for the existence of a value that matches the key. If this is found, an update should be triggered (this fails), if not found, then an insert is done (this works).
{
Table table = jcsSession.getTableByName("TEST_TABLE");
String searchKey = "1000";
String key = "1000";
boolean boolFound = false;
if (table != null) {
for (Iterator it = table.getTableRowByKey(searchKey); it.hasNext();) {
TableValue tv = (TableValue)it.next();
bFound = true;
}
}
if (boolFound) {
// Updating value
jcsOut.println("Found an entry, updating");
TableValue tv = table.createTableValue();
tv.setSearchKey(key);
tv.setColumnName("DESCRIPTION");
tv.setColumnValue("Updated description");
jcsSession.persist();
}
else {
// Insert value
jcsOut.println("Inserting new entry");
TableValue tv = table.createTableValue();
tv.setColumnName("DESCRIPTION");
tv.setKey(key);
tv.setColumnValue("New description");
tv.setSearchKey(key);
jcsSession.persist();
}
}
Error is then:
Caused by: JCS-102313: SearchKey: 1000 can not be different than: null
If using the setKey method, the failure raised relates to a duplicate key field, asn the createTableValue() seems to be intended only to insert new values into tables.
Thanks in advance.
Edited by: JoStew on Nov 28, 2011 1:45 PM