Beckhoff-根据另一个查询的结果构建查询

时间:2019-04-10 16:01:28

标签: string types string-concatenation twincat

我想从Twicat3 PLC环境中的查询结果构建SELECT语句。

当对变量进行硬编码时,查询可以正确执行,但是当从另一个查询的结果传递变量时,查询将无法执行。定义的数据类型是相同的。 InformationLog视图中的错误指示“词典中不存在给定的键”。使用第一个查询的结果执行查询时。

PROGRAM MAIN
VAR
conv_QueryDB    : Query_tblConveyorQueue;
conv_queryRes   : ARRAY [0..9] OF tblConveyorQueue;
conv_str_query  : STRING(2000);
conv_query      : WSTRING(2000);
conv_query1     : WSTRING(255) := "SELECT TOP 1 LEFT(AreaID,5) FROM ConveyorQueue WHERE CartonID = '";
conv_barcode    : WSTRING(255) := "900981-1";
conv_query2 : WSTRING(255) := "' ORDER BY AreaID";

zone_QueryDB    : Query_tblZoneMap;
zone_queryRes   : ARRAY [0..18] OF tblZoneMap;
zone_str_query  : STRING(2000);
zone_query  : WSTRING(2000);
zone_query1 : WSTRING(255) := "SELECT transfer, zone, direction, neighborZone, deliveryLoc FROM tblZoneMap WHERE transfer = 1 AND zone = 0 AND deliveryLoc = '";
zone_dest       : WSTRING(255);
zone_query2 : WSTRING(255) := "' ";
END_VAR

conv_query := WCONCAT(conv_query1, WCONCAT(conv_barcode, conv_query2));
conv_str_query := WSTRING_TO_STRING(conv_query);
conv_QueryDB(queryString := conv_str_query, dbConn := 2, bConnect := 1);
conv_queryRes := conv_QueryDB.queryResults;

zone_dest := STRING_TO_WSTRING(conv_queryRes[0].AreaID); 
//zone_dest := "P1030"; //This works when uncommented??!!??
zone_query := WCONCAT(zone_query1, WCONCAT(zone_dest, zone_query2));
zone_str_query := WSTRING_TO_STRING(zone_query);
zone_QueryDB(queryString := zone_str_query, dbConn := 1, bConnect := 1);
zone_queryRes := zone_QueryDB.queryResults;


我希望将zone_dest作为硬编码值执行时与从conv_queryRes数组中提取时,zone_queryRes相同。我不确定这是否是数据类型问题。执行后,查询字符串似乎具有正确的语法。

-------------- EDIT -------------------------------- ---------------------------------

包括SQL功能块。

FUNCTION_BLOCK Query_tblZoneMap
VAR_INPUT 
    queryString     : STRING(2000); //Select statement
    dbConn          : UDINT; //Int of connection configured in TF6420
    bCONNECT        : BOOL; //Rising trigger to connect to db. Need to make this re-fireable

END_VAR
VAR_OUTPUT
    queryResults    : ARRAY [0..18] OF tblZoneMap;
    nError          : INT;

END_VAR
VAR
    fbSQLDatabase   : FB_SQLDatabase(sNetID:='', tTimeout:=T#500MS);
    fbSqlCommand    : FB_SQLCommand(sNetID := '', tTimeout := T#500MS);     
    fbSQLResult     : FB_SQLResult(sNetID:='', tTimeout:=T#500MS);
    nState          : INT;
    R_TRIG1         : R_TRIG;
END_VAR


(*
    Welcome to the QueryDB function block!
    This function exists to query a database that is configured using the TF6420 Connectivity module. 
    When executed, it establishes a connection with the database, creates an instance of the FB_SQLCommand,
    executes the instance of FB_SQLCommand, then reads the cached results using FB_SQLResult into the 
    queryResults array.
    When results are received successfully, the database connection is closed.

*)
R_TRIG1(CLK:=bCONNECT);
IF R_TRIG1.Q AND nState = 0 THEN
    nState := 1;

END_IF
CASE nState OF
    0:(*Idle*)
    IF bConnect THEN
        bCONNECT := 0;
    END_IF  
    1: // Connect to database 
    IF fbSqlDatabase.Connect(dbConn) THEN
        IF fbSqlDatabase.bError THEN
            nState := 255;
        ELSE
            nState := nState+1;
        END_IF
    END_IF  

    2: // Create a command reference
    IF fbSqlDatabase.CreateCmd(ADR(fbSqlCommand)) THEN
        IF fbSqlDatabase.bError THEN
            nState := 255;
        ELSE
            nState := nState+1;
        END_IF
    END_IF

    3://Execute SELECT Statement FB_SQLCommand.ExecuteDataReturn(pSQLCmd:= , cbSQLCmd:= , pSQLDBResult:= )
    IF fbSQLCommand.ExecuteDataReturn(pSQLCmd:= ADR(queryString)
                                    , cbSQLCmd:= SIZEOF(queryString)
                                    , pSQLDBResult:= ADR(fbSqlResult)) THEN
        IF fbSQLCommand.bError THEN
            nState := 255;
            ELSE
            nState := nState + 1;
        END_IF
    END_IF

    4://Read Results 
    IF fbSqlResult.Read(nStartIndex:= 0
                        ,nRecordCount:= 19
                        ,pData:= ADR(queryResults)
                        ,cbData:= SIZEOF(queryResults)
                        ,bWithVerifying:= FALSE
                        ,bDataRelease:= FALSE) THEN
        IF fbSqlResult.bError THEN
            nState := 255;
        ELSE
            nState := nState+1;
        END_IF
    END_IF  
    5:// disconnect from database
    IF fbSqlDatabase.Disconnect() THEN
        IF fbSqlDatabase.bError THEN
            nState := 255;
        ELSE
            nState := 0;
            bConnect := 0;
        END_IF
    END_IF
    255:
        nError  := 1; // If errored, check SQL logs.
        bConnect := 0;
        nState := 0;
END_CASE

2 个答案:

答案 0 :(得分:0)

为什么首先使用WSTRING? 使用普通的字符串,这样就不必来回转换,并且代码更易读。

如果将多个String并置在一起,请构建一个具有此功能的函数。 调用时看起来像这样:

F_CONCAT3(
    zone_query1,
    zone_dest,
    zone_query2,
);

我建议您使用Beckhoff编码约定,以便其他开发人员也可以阅读您的代码:

https://infosys.beckhoff.com/english.php?content=../content/1033/tc3_plc_intro/45035999420423563.html&id=

您的代码将如下所示:

sConvQuery := F_CONCAT3(sConvQuery1, sConvBarcode, sConvQuery2);

//Use Enums instead of numbers 
fbQueryDB(queryString := sConvQuery , dbConn := 2, bConnect := TRUE);
aConvQueryRes := fbQueryDB.queryResults;

sZoneDest := aConvQueryRes[0].AreaID; 
sZoneQuery := F_CONCAT3(sZoneQuery1,sZoneDest,sZoneQuery2);

fbQueryDB(queryString := sZoneQuery, dbConn := 1, bConnect := TRUE);
aZoneQueryRes := fbQueryDB.queryResults;

能否请您发布zone_QueryDB功能块的代码?

答案 1 :(得分:0)

我还认为您应该按顺序放置查询,以避免Query_tblZoneMap功能块中发生任何冲突:

CASE nStep OF

    eFIRST_QUERY: 

        sConvQuery := F_CONCAT3(sConvQuery1, sConvBarcode, sConvQuery2);
        //Use Enums instead of numbers 
        fbQueryDB(queryString := sConvQuery , dbConn := 2, bConnect := TRUE);
        nStep := eWAIT_FOR_FIRST_RESULT;

    eWAIT_FOR_FIRST_RESULT:     

        IF NOT fbQueryDB.bBusy THEN
            aConvQueryRes := fbQueryDB.queryResults;
            nStep := eSECOND_QUERY;
        END_IF

        //Error handling 
        //ELSIF fbQueryDB.bError ...

    eSECOND_QUERY:

        sZoneDest := aConvQueryRes[0].AreaID; 
        sZoneQuery := F_CONCAT3(sZoneQuery1,sZoneDest,sZoneQuery2);
        fbQueryDB(queryString := sZoneQuery, dbConn := 1, bConnect := TRUE);
        nStep := eWAIT_FOR_SECOND_RESULT;

    eWAIT_FOR_SECOND_RESULT:    

        IF NOT fbQueryDB.bBusy THEN
            aZoneQueryRes := fbQueryDB.queryResults;
            nStep := eCHECK_RESULT;
        END_IF

        //Error handling
        //ELSIF fbQueryDB.bError ...

    eCHECK_RESULT: //...    

END_CASE