Use a Database Device
- Last UpdatedJul 18, 2023
- 2 minute read
You can use a database device to do the following:
Write dBase Records
To write data to a database device, first append a record to the end of the device, then add data to the fields of the record. For a dBASE database, the DevAppend() function appends the record, and the DevSetField() function writes data to a field. The following function writes a recipe record to a device:
FUNCTION
WriteRecipeData(INT hDevice, STRING sName, INT Water, INT Sugar,
T Flour, INT Salt, INT Yeast, INT Milk)
DevAppend(hDevice);
DevSetField(hDevice, "NAME", sName);
DevSetField(hDevice, "WATER", IntToStr(Water));
DevSetField(hDevice, "SUGAR", IntToStr(Sugar));
DevSetField(hDevice, "FLOUR", IntToStr(Flour));
DevSetField(hDevice, "SALT", IntToStr(Salt));
DevSetField(hDevice, "YEAST", IntToStr(Yeast));
DevSetField(hDevice, "MILK", IntToStr(Milk));
END
Write SQL Records
To use an SQL device in Plant SCADA, you cannot use every the Cicode Device function; you can only use the following functions:
|
DevOpen() |
DevClose() |
DevGetField() |
DevFind() |
DevWrite() |
|
DevNext() |
DevSeek() |
DevAppend() |
DevWrite() |
DevZap() |
|
DevControl() |
DevSetField() |
To write Plant SCADA data to an SQL database, use the DevWrite() function, but you need to add a new record and write to fields in the new record. No data is written to the database if you do not write to all fields.
For example:
FUNCTION
WriteRecipeData(INT hDevice, STRING sName, INT Water, INT Sugar,
INT Flour, INT Salt, INT Yeast, INT Milk)
DevWrite(hDevice, sName);
DevWrite(hDevice, Water);
DevWrite(hDevice, Sugar);
DevWrite(hDevice, Flour);
DevWrite(hDevice, Salt);
DevWrite(hDevice, Yeast);
DevWrite(hDevice, Milk);
END
The following functions return error 267 (File mode is invalid) when the device type is SQL:
|
DevFlush() |
|
DevPrev() |
|
DevDelete() |
The follow function returns error 263 (Cannot read file) when the device type is SQL:
|
DevRead() |
The following functions return error -1 when the device type is SQL:
|
DevSize() |
|
DevRecNo() |
Locate and Read Database Records
To read data from a dBASE or SQL database device, use the DevFind() function to locate the record, and then the DevGetField() function to read each field:
FUNCTION
GetRecipe(STRING sName)
INT hDev;
hDev = DevOpen("Recipe");
IF hDev >= 0 THEN
IF DevFind(hDev, sName, "NAME") = 0 THEN
PLC_Water = DevGetField(hDev, "WATER");
PLC_Sugar = DevGetField(hDev, "SUGAR");
PLC_Flour = DevGetField(hDev, "FLOUR");
PLC_Salt = DevGetField(hDev, "SALT");
PLC_Yeast = DevGetField(hDev, "YEAST");
PLC_Milk = DevGetField(hDev, "MILK");
ELSE
DspError("Cannot Find Recipe " + sName);
END
DevClose(hDev);
ELSE
DspError("Cannot open recipe database");
END
END
Delete Records
You can delete dBASE records with the DevDelete() function. The following Cicode function deletes records from a dBASE device:
FUNCTION DeleteRecords(INT hDev)
WHILE NOT DevEOF(hDev) DO
DevDelete(hDev);
DevNext(hDev);
END
END
To delete every record from a dBASE database, use the DevZap() function:
FUNCTION DeleteRecords(INT hDev)
DevZap(hDev);
END
To delete records from an SQL database, use the Cicode SQL functions.
To open a database device or devices:
Use the DevOpen() function. this function returns an integer handle to identify each device, as in the following example:
INT hRecipe;
hRecipe = DevOpen("Recipe");
When you are finished with a device, close it to free Cicode system resources by using the DevClose() function. For example:
DevClose(hRecipe);