You are on page 1of 4

Advantech AE Technical Share Document

Date 2015 / 12 / 09 SR#


Category ■ FAQ □ SOP Related OS N/A
Abstract How to use a TCL script to read and write data from ODBC Database
Keyword TCL, ODBC, WebAccess, Database.
Related Product N/A

 Problem Description:
How to use a TCL script to read and write data from ODBC Database for WebAccess ?

 Brief Solution - Step by Step:

1 Open ODBC database ODBC connection :


TCL script:
SQLDIRECT "CONNECT SESSION 1 TO bwpdata_Access"
SQLDIRECT "USE SESSION 1"
Explanation:
SESSION 1 : The number of connection
Bwpdata_Access : ODBC DSN name, it must be re-set in the ODBC administrator of control panel

2 Write data to the ODBC database


TCL script:
SQLDIRECT "INSERT INTO testing VALUES ('[GETVAL %TTMTIME]','[GETVAL %ATMMINUTE]')"
Explanation:
%TTMTIME : Time in Hour:Minute:Second is 24 hours format. (Example 00:00:01 / 01:02:01)
%ATMMINUTE : Minute of the Hour. (0 to 59)

Page 1
3 Calculation the amount of data
TCL script:
SQLPREPARE "DECLARE C1 CURSOR FOR"
SQLPREPARE "SELECT count(*) FROM testing"
SQLEXECUTE
SQLDIRECT "OPEN C1"
SQLDIRECT "FETCH NEXT C1 INTO Counter"

Explanation:
Use “CURSOR” to obtain the amount of data
“count(*)” is SQL command to obtain the amount of data
“testing” is name of data table
“C1” is cursor name
“Counter” is constant Tag in WebAccess

Page 2
4 Read data to the ODBC database
TCL script :
SQLPREPARE "DECLARE C1 CURSOR FOR"
SQLPREPARE "SELECT myValue FROM testing"
SQLEXECUTE

for {set i 0} {$i<=9} {incr i} {


SETVAL myArray($i)=0}

SQLDIRECT "OPEN C1"


for {set i 0} {$i<=9} {incr i} {
SQLDIRECT "FETCH NEXT C1 INTO myArray($i)"
}
Explanation:
myArray is an array of constant Tag, means myArray(0)~ myArray(9)
Select top 10 data from ODBC database into array tags, examples myArray(0)~ myArray (9)

Page 3
5 Close ODBC database connection :
TCL script :
SQLDIRECT "DISCONNECT SESSION 1"

Explanation:
SESSION 1 : The number of ODBC connection

 Reference:
1. 12.10.11 Read database into an Array for Display with SQL
2. 12.10.12 Write data into an ODBC database

Page 4

You might also like