Applies To:
  • CitectSCADA 3.x, 4.x

Summary:
How do I get the numeric value of I/O device variable tags into a database using SQL? To add a record to a table, I used the example in the Citect Programmers Reference manual but still cant get the tag values to appear in the database table. I keep getting errors like;

"4101 The connection, statement, or query handle you provided is not valid."

 

Solution:
  1. Get the PLC tags into your function and convert them into strings. See the simplified example below.
  2. Use SQLSet (sets up a buffer) as the first function to build up the first part of the string;

INSERT INTO TABLE ,tag2,tag3,tag4)VALUES(

  1. Then using SQLAppend, you build up an SQL statement (you could use one SQLAppend per Tag or String).
  2. Cicode puts the value as a string into the SQL query, a query is a string.
  3. Instead of SQLExec now use SQLExec(hSQL, ""); it then knows to use the buffered SQL query.

STRING sSQL;
INT hSQL=-1;

FUNCTION
Insert()
   String st1=intToStr(CPU_Usage);
   String st2=intToStr(FILTER_TANK_LEV);
   String st3=intToStr(SugarPLC);
   Int Status=0;

   hSQL=SQLconnect("DSN=Test");
   If hSQL<0 Then
      Message("Connect error", SqlerrMsg(), 48);
   Else
      SQLSet(hSQL, "INSERT INTO TABLE1 (Tag1,tag2,tag3,tag4)VALUES(");
      SQLAppend(hSQL, st1+","+st2+","+st3+",'Test string')");
      Status=SQLExec(hSQL, "");
      If Status <> 0 Then
         Message("Status error", SqlerrMsg(), 48);
      End
   End
   SQLDisconnect(hSQL);
END

The above cicode produces the following in a Microsoft Access database, fields Tag1 to Tag3 are numeric fields and Tag4 is a string field;

Tag1 Tag2 Tag3 Tag4
15 37 15 Test string
16 42 15 Test string
2 48 15 Test string
6 51 15 Test string
 

Keywords:
 

Attachments